这个无限滚动的脚本中发生了什么?

发布于 2024-12-16 02:10:31 字数 2029 浏览 3 评论 0原文

我发现了这个无限滚动脚本。它有效,但我不理解这一行:

'contentData': {}, // you can pass the children().size() to know where is the pagination

当我将其更改为这样的内容时:

'contentData': {xyz:($('#content').children().size())}, 

每次值都是相同的。在我的示例中,当我在 afterLoad 部分中调用 alert(($('#content').children().size())) 时,该值是正确的(每个滚动事件都不同)。我不明白如何将 contentData 设置为不同的值(例如第一次加载时为 10,第二次加载时为 20,等等)。

这是我的脚本:

$(function(){
    $('#content').scrollPagination({
        'contentPage': '/democontent.php', // the page where you are searching for results
        'contentData': {xyz:($('#content').children().size())}, // you can pass the children().size() to know where is the pagination
        'scrollTarget': $(window), // who gonna scroll? in this example, the full window
        'heightOffset': 10, // how many pixels before reaching end of the page would loading start? positives numbers only please
        'beforeLoad': function(){ // before load, some function, maybe display a preloader div
            $('#loading').fadeIn(); 
        },

        'afterLoad': function(elementsLoaded){ // after loading, some function to animate results and hide a preloader div
            $('#loading').fadeOut();
            var i = 0;
            $(elementsLoaded).fadeInWithDelay();
            alert(($('#content').children().size()));

            if ($('#content').children().size() > 10000){ // if more than 100 results loaded stop pagination (only for test)
                $('#nomoreresults').fadeIn();
                $('#content').stopScrollPagination();
            }
        }
    });

    // code for fade in element by element with delay
    $.fn.fadeInWithDelay = function(){
        var delay = 0;
        return this.each(function(){
            $(this).delay(delay).animate({opacity:1}, 200);
            delay += 100;
        });
    };
});

I found this infinite scroll script. It works, but I don't understand this line:

'contentData': {}, // you can pass the children().size() to know where is the pagination

when I change it to something like this:

'contentData': {xyz:($('#content').children().size())}, 

The value is the same every time. In my example, when I call alert(($('#content').children().size())) in the afterLoad section, the value is correct (on every scrollevent different). I don't understand how to set contentData to different values (like 10 on the first load, 20 on the second load, etc.).

Here's my script:

$(function(){
    $('#content').scrollPagination({
        'contentPage': '/democontent.php', // the page where you are searching for results
        'contentData': {xyz:($('#content').children().size())}, // you can pass the children().size() to know where is the pagination
        'scrollTarget': $(window), // who gonna scroll? in this example, the full window
        'heightOffset': 10, // how many pixels before reaching end of the page would loading start? positives numbers only please
        'beforeLoad': function(){ // before load, some function, maybe display a preloader div
            $('#loading').fadeIn(); 
        },

        'afterLoad': function(elementsLoaded){ // after loading, some function to animate results and hide a preloader div
            $('#loading').fadeOut();
            var i = 0;
            $(elementsLoaded).fadeInWithDelay();
            alert(($('#content').children().size()));

            if ($('#content').children().size() > 10000){ // if more than 100 results loaded stop pagination (only for test)
                $('#nomoreresults').fadeIn();
                $('#content').stopScrollPagination();
            }
        }
    });

    // code for fade in element by element with delay
    $.fn.fadeInWithDelay = function(){
        var delay = 0;
        return this.each(function(){
            $(this).delay(delay).animate({opacity:1}, 200);
            delay += 100;
        });
    };
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Smile简单爱 2024-12-23 02:10:31

我刚刚使用这段代码并遇到了同样的问题。然后我在我们使用的 .js 文件中寻找答案:scrollpagination.js

在这个文件中定义了一些 jQuery 函数。
第一个是:

$.fn.scrollPagination.loadContent = function(obj, opts){...}

这是每次滚动足够底部时调用我们的页面(目标)的页面。事实上,它只定义一次,所以如果你给出像 $("#targetDiv").children().size() 这样的参数,它就会使用一次并每次将第一个值放入目标中。

这个想法是传递一个参数,该参数将在 javascript(这里是 jQuery)中使用来更新值:一个函数;

基本上你只需要这样做:

'contentData': {xyz:function() {return $('#content').children().size()}},

每次使用该函数时,它都会计算返回值。

希望这有帮助,请原谅我糟糕的英语。

I just worked with this piece of code and had the same problem. Then I looked for answer in the .js file we use : scrollpagination.js

In this some jQuery functions are defined.
And the first is :

$.fn.scrollPagination.loadContent = function(obj, opts){...}

This is the one which call our page (target) each time the scroll is in the bottom enough. In fact this is defined once and only once, so if you give argument like $("#targetDiv").children().size(), it will take this once and put the first value in the target each time.

The idea is to pass an argument which will be used in javascript (here jQuery) to update the value : a function;

Basically you just have to do this :

'contentData': {xyz:function() {return $('#content').children().size()}},

And each time the function will be used it will calculate the return value.

Hope this helped, and forgive please forgive my poor english.

臻嫒无言 2024-12-23 02:10:31

如果您想跟踪脚本正在执行多少负载。试试这个:

$(function(){
    var loads = 0;
    $('#content').scrollPagination({
        'contentPage': '/democontent.php?loads='+loads, // the page where you are searching for results
        'contentData': {}, // you can pass the children().size() to know where is the pagination
        'scrollTarget': $(window), // who gonna scroll? in this example, the full window
        'heightOffset': 10, // how many pixels before reaching end of the page would loading start? positives numbers only please
        'beforeLoad': function(){ // before load, some function, maybe display a preloader div
            $('#loading').fadeIn(); 
        },

        'afterLoad': function(elementsLoaded){ // after loading, some function to animate results and hide a preloader div
             $('#loading').fadeOut();
             var i = 0;
             loads++;
             alert('Number of loads is now: '+loads);
             $(elementsLoaded).fadeInWithDelay();
                alert(($('#content').children().size()));
             if ($('#content').children().size() > 10000){ // if more than 100 results loaded stop pagination (only for test)
                $('#nomoreresults').fadeIn();
                $('#content').stopScrollPagination();
             }
        }
    });

    // code for fade in element by element with delay
    $.fn.fadeInWithDelay = function(){
        var delay = 0;
        return this.each(function(){
            $(this).delay(delay).animate({opacity:1}, 200);
            delay += 100;
        });
    };

});
</script> 

If you want to keep track of how many loads your script is doing. Try this:

$(function(){
    var loads = 0;
    $('#content').scrollPagination({
        'contentPage': '/democontent.php?loads='+loads, // the page where you are searching for results
        'contentData': {}, // you can pass the children().size() to know where is the pagination
        'scrollTarget': $(window), // who gonna scroll? in this example, the full window
        'heightOffset': 10, // how many pixels before reaching end of the page would loading start? positives numbers only please
        'beforeLoad': function(){ // before load, some function, maybe display a preloader div
            $('#loading').fadeIn(); 
        },

        'afterLoad': function(elementsLoaded){ // after loading, some function to animate results and hide a preloader div
             $('#loading').fadeOut();
             var i = 0;
             loads++;
             alert('Number of loads is now: '+loads);
             $(elementsLoaded).fadeInWithDelay();
                alert(($('#content').children().size()));
             if ($('#content').children().size() > 10000){ // if more than 100 results loaded stop pagination (only for test)
                $('#nomoreresults').fadeIn();
                $('#content').stopScrollPagination();
             }
        }
    });

    // code for fade in element by element with delay
    $.fn.fadeInWithDelay = function(){
        var delay = 0;
        return this.each(function(){
            $(this).delay(delay).animate({opacity:1}, 200);
            delay += 100;
        });
    };

});
</script> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文