How to create infinite iscroll?

雨燕双飞 提交于 2020-01-06 05:40:10

问题


I created iScroll on my webpage and it was working very well. But now i want to make it work as infinite iscroll, but i don't know how to do that.

my iscroll code is:

myCarousel_up = new iScroll('scroller_upCarousel', {
        snap: true,
        momentum: false,
        hScrollbar: false,
                vScrollbar: false,
        desktopCompatibility:true,
        onScrollEnd: function () {

            }
    });

Can anyone help me?


回答1:


First you need to add iscroll-infinite.js

then you need to write ajax function to load or append data in infinite loop.

function ajax (url, parms) {
parms = parms || {};
var req = new XMLHttpRequest(),
    post = parms.post || null,
    callback = parms.callback || null,
    timeout = parms.timeout || null;

req.onreadystatechange = function () {
    if ( req.readyState != 4 ) return;

    // Error
    if ( req.status != 200 && req.status != 304 ) {
        if ( callback ) callback(false);
        return;
    }

    if ( callback ) callback(req.responseText);
};

if ( post ) {
    req.open('POST', url, true);
    req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
} else {
    req.open('GET', url, true);
}

req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

req.send(post);

if ( timeout ) {
    setTimeout(function () {
        req.onreadystatechange = function () {};
        req.abort();
        if ( callback ) callback(false);
    }, timeout);
}
}

Also call iScroll loaded function with ajax function

var myScroll;

function loaded () {
myScroll = new IScroll('#wrapper', {
    mouseWheel: true,
    infiniteElements: '#scroller .row',
    //infiniteLimit: 2000,
    dataset: requestData,
    dataFiller: updateContent,
    cacheSize: 1000
});
}

function requestData (start, count) {
ajax('dataset.php?start=' + +start + '&count=' + +count, {
    callback: function (data) {
        data = JSON.parse(data);
        myScroll.updateCache(start, data);
    }
});
}

function updateContent (el, data) {
el.innerHTML = data;
}

document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); 

Please refer below link for demo.

http://lab.cubiq.org/iscroll5/demos/infinite/




回答2:


You could use ajax functoin like:

myCarousel_up = new iScroll('scroller_upCarousel', {
        snap: true,
        momentum: false,
        hScrollbar: false,
        vScrollbar: false,
        desktopCompatibility:true,
        onScrollEnd: function () {
        if($('#scroller_upCarousel').hasClass('scrolling')) {
            $('#scroller_upCarousel').removeClass('scrolling');
        } 
        ajaxActionToGetMoreList(); //Execute custom function(ajax call)
    }
});


来源:https://stackoverflow.com/questions/10974285/how-to-create-infinite-iscroll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!