查看插件的源代码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);
}
};
说明:当您向下滚动页面(连续或离散)时,“滚动”事件会一遍又一遍地触发并调用handler: function(...)
上面代码中的 。在此方法内部,设置了一个计时器,该计时器setTimeout
将在触发滚动事件 100 毫秒后触发相应的无限滚动代码。但是,这个计时器也在方法内部被取消{ clearTimeout(scrollTimeout); }
。
因此,只要用户继续滚动,计时器就会不断清零并重新设置,直到用户停止滚动。在最后一个滚动事件中,计时器被设置并且永远不会被清除,因此它将在 100 毫秒后触发并执行内容加载。我在上面演示的编辑删除了这个等待期并在用户到达页面底部时加载内容(即使他们仍在积极滚动)。
注意:作者可能打算将此超时用于性能问题,但欢迎您在配置中对其进行测试。