我有用於加載數據的代碼。當用戶向下滾動一半屏幕時,它將繼續加載數據並將HTML附加到文檔中。 The problem is $(window).height() doesn't update on the fly after HTML changes
所以條件表達式出錯了。如何解決它?
// Check scrollbar is down a half.
$(window).scroll(function() {
console.log("flag: " + flag);
console.log($(window).scrollTop());
console.log($(window).height());
if (($(window).scrollTop() > $(window).height()/2)) {
if(flag == 0) {
console.log("Load data");
loadData(globalIndex, globalCount);
globalIndex += 40;
}
flag = 1;
} else {
flag = 0;
}
});
試試這個插件:https://gist.github.com/elclanrs/5514942。你可以設置'offset'和其他一些選項,就像1k縮小一樣。 – elclanrs
我想也許你想'$(document).height()' – Barmar
順便說一句,訣竅在這裏:'endOfPage = $(doc).height() - $(win).height(); if($(win).scrollTop()> = endOfPage - offset)' – elclanrs