3 回答

TA貢獻1836條經驗 獲得超3個贊
可能是滾動事件連續多次觸發。因此,在實際觸發 if 中的代碼之前,當 window.scrollTop() 距底部 800,然后距底部 799,距底部 798 時,將調用您的函數。
嘗試重新設置 window.scrollTop(value) 以便它永遠不會從底部超過 800。這樣你的函數只被調用一次。

TA貢獻1818條經驗 獲得超8個贊
您正在使用“if”條件,只要 $(window).scrollTop() + $(window).height() > $(document).height() - 800 這意味著每當
$(window).scrollTop() + $(window).height()
低于
$(document).height() - 800
該函數將始終被調用。嘗試使用 '===' 而不是 '>' 這樣循環只會針對特定值觸發一次。
您也可以嘗試使用控件??梢哉f; 介紹
var hasRun = 0; //increment this value once the condition
// $(window).scrollTop() + $(window).height() > $(document).height() - 800
//is true.
每次減少 var hasRun 的值
$(window).scrollTop() + $(window).height() < $(document).height() - 800
是真的。請試試這個。
$(window).scroll(function(event) {
var hasRun = 0;
if($(window).scrollTop() + $(window).height() > $(document).height() -
800 ) {
hasRun = hasRun + 1;
}
if($(window).scrollTop() + $(window).height() < $(document).height() -
800 ) {
hasRun = 0;
}
if(hasRun <= 0){
// ajax call get data from server and append to the div
var id = $('#load_more_button').data('id');
$('#load_more_button').html('<b>Loading...</b>');
if (id >= 1) {
load_data(id, _token);
}
}
});
添加回答
舉報