縱觀該插件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
,它將在滾動事件被觸發後100ms觸發適當的無限滾動代碼。但是,該計時器在方法{ clearTimeout(scrollTimeout); }
內也被取消。
因此,只要用戶保持滾動,定時器就會一直清除並重新設置,直到用戶停止滾動爲止。在最後一次滾動事件中,定時器被設置並且從不清除,因此它將在100ms後觸發並執行內容加載。我上面演示的編輯刪除了這個等待期,並在用戶到達頁面底部時加載內容(即使它們仍在活動滾動)。
注意:作者可能希望在性能問題上超時,但歡迎您在配置中對其進行測試。
非常感謝。我想我可以通過在新內容的dom內容被加載之前保持「工作」標誌打開來避免性能降低,忽略標誌仍然打開時的任何進一步的滾動事件,而不是依賴於定時器。它應該希望保持較低的開銷並且仍然能夠達到預期的效果。顯然需要更多的工具,但這是我一直在尋找的,再次感謝! – oucil