Javascript 什么';在这个无限滚动脚本中发生了什么?

Javascript 什么';在这个无限滚动脚本中发生了什么?,javascript,jquery,infinite-scroll,Javascript,Jquery,Infinite Scroll,我找到了这个。行得通,但我不明白这句话: 'contentData': {}, // you can pass the children().size() to know where is the pagination 当我将其更改为以下内容时: 'contentData': {xyz:($('#content').children().size())}, 每次的值都是相同的。在我的示例中,当我在afterLoad部分调用alert(($('#content').children().si

我找到了这个。行得通,但我不明白这句话:

'contentData': {}, // you can pass the children().size() to know where is the pagination
当我将其更改为以下内容时:

'contentData': {xyz:($('#content').children().size())}, 
每次的值都是相同的。在我的示例中,当我在
afterLoad
部分调用
alert(($('#content').children().size())
时,值是正确的(在每个scrollevent上都是不同的)。我不知道如何将
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;
        });
    };
});

如果要跟踪脚本正在执行的加载次数。试试这个:

$(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> 
$(函数(){
无功负荷=0;
$(“#内容”)。滚动分页({
“contentPage”:“/democontent.php?load=”+加载,//搜索结果的页面
'contentData':{},//您可以传递children().size()来知道分页的位置
'scrollTarget':$(窗口),//谁将滚动?在本例中,是完整窗口
“heightOffset”:10,//在到达页面末尾之前,将开始加载多少像素?请仅输入正数
“beforeLoad”:函数(){//在加载之前,某些函数可能会显示预加载程序div
$('#加载').fadeIn();
},
“afterLoad”:函数(elementsLoaded){//加载后,一些函数用于设置结果动画和隐藏预加载程序div
$(“#加载”).fadeOut();
var i=0;
加载++;
警报('负载数量现在为:'+负载);
$(elementsLoaded.fadeInWithDelay();
警报($('content').children().size());
if($('#content').children().size()>10000){//if加载的结果超过100则停止分页(仅用于测试)
$('#nomoreresults').fadeIn();
$('#content')。停止滚动分页();
}
}
});
//具有延迟的逐元素淡入代码
$.fn.fadeInWithDelay=函数(){
var延迟=0;
返回此值。每个(函数(){
$(this.delay(delay).animate({opacity:1},200);
延迟+=100;
});
};
});

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

本文定义了一些jQuery函数。 第一个是:

$.fn.scrollPagination.loadContent = function(obj, opts){...}
这是一个调用我们的网页(目标)每次滚动是在底部足够。事实上,它只定义了一次,所以如果您给出像$(“#targetDiv”).children().size()这样的参数,它将接受一次,并每次将第一个值放入目标中

其思想是传递一个参数,该参数将在javascript(这里是jQuery)中用于更新值:一个函数

基本上,你只需要这样做:

'contentData': {xyz:function() {return $('#content').children().size()}},
每次使用该函数时,它都会计算返回值


希望这有帮助,请原谅我的英语不好。

如果从{}中删除
xyz:($('#content').children().size())
,会发生什么?它不在实际例子中。上面说你可以传递信息。当我删除xyz时没有必要:($('#content').children().size())democontent.php不接收$#u POST数据xyz。我想使用这些POST数据来增加mysql查询中的限制。返回的内容中是否包含此脚本?如果是这样,看起来您可以在脚本顶部将每次加载设置为0。我不确定脚本的内部结构,但它使用的是没有选择器的.load(page)[很多无限滚动程序都使用这种技术],然后它真正运行.html(),并在删除脚本之前执行脚本。请参阅.load()文档。