查看插件https://raw.github.com/paulirish/infinite-scroll/master/jquery.infinitescroll.js的源码,我想说需要修改如下代码:
event.special.smartscroll = {
setup: function () {
$(this).bind("scroll", event.special.smartscroll.handler);
},
teardown: function () {
$(this).unbind("scroll", event.special.smartscroll.handler);
},
handler: function (event, execAsap) {
// Save the context
var context = this,
args = arguments;
// set correct event type
event.type = "smartscroll";
if (scrollTimeout) { clearTimeout(scrollTimeout); }
scrollTimeout = setTimeout(function () {
$.event.handle.apply(context, args);
}, execAsap === "execAsap" ? 0 : 100);
}
};
在这一行进行编辑}, execAsap === "execAsap" ? 0 : 100);:
event.special.smartscroll = {
setup: function () {
$(this).bind("scroll", event.special.smartscroll.handler);
},
teardown: function () {
$(this).unbind("scroll", event.special.smartscroll.handler);
},
handler: function (event, execAsap) {
// Save the context
var context = this,
args = arguments;
// set correct event type
event.type = "smartscroll";
if (scrollTimeout) { clearTimeout(scrollTimeout); }
scrollTimeout = setTimeout(function () {
$.event.handle.apply(context, args);
}, 0);
}
};
解释:当您向下滚动页面(连续或不连续)时,'scroll' 事件会反复触发并调用上述代码中的handler: function(...)。在此方法内部,设置了一个计时器setTimeout,它将在触发滚动事件后 100 毫秒触发相应的无限滚动代码。然而,这个定时器也在方法{ clearTimeout(scrollTimeout); }中被取消了。
因此,只要用户继续滚动,计时器就会不断清零并重新设置,直到用户停止滚动。在最后一个滚动事件中,计时器被设置并且永远不会被清除,因此它将在 100 毫秒后触发并执行内容加载。我在上面演示的编辑删除了这个等待期并在用户到达页面底部时加载内容(即使他们仍在积极滚动)。
注意:作者可能打算将此超时用于性能问题,但欢迎您在配置中对其进行测试。