3 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊
可能是滾動(dòng)事件連續(xù)多次觸發(fā)。因此,在實(shí)際觸發(fā) if 中的代碼之前,當(dāng) window.scrollTop() 距底部 800,然后距底部 799,距底部 798 時(shí),將調(diào)用您的函數(shù)。
嘗試重新設(shè)置 window.scrollTop(value) 以便它永遠(yuǎn)不會(huì)從底部超過 800。這樣你的函數(shù)只被調(diào)用一次。

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊
您正在使用“if”條件,只要 $(window).scrollTop() + $(window).height() > $(document).height() - 800 這意味著每當(dāng)
$(window).scrollTop() + $(window).height()
低于
$(document).height() - 800
該函數(shù)將始終被調(diào)用。嘗試使用 '===' 而不是 '>' 這樣循環(huán)只會(huì)針對(duì)特定值觸發(fā)一次。
您也可以嘗試使用控件??梢哉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
是真的。請(qǐng)?jiān)囋囘@個(gè)。
$(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);
}
}
});
添加回答
舉報(bào)