虽然其他答案可以告诉您何时到达底部,但为了回答您关于如何判断何时接近底部的问题,我以前使用过这个:
if ( ($(document).height() - $(window).height()) - $(window).scrollTop() < 1000 ){
//do stuff
}
你可以将值“1000”更改为任何你想要的值,以便在距离底部该距离时触发你的脚本。
$(window).scroll(function(){
if ($(window).scrollTop() == $(document).height()-$(window).height()){
alert("We're at the bottom of the page!!");
}
});
window.scrollY
和window.innerHeight
代替它们的jQuery对应项将解决这两个问题。 - tvancwindow.scrollY
不是完全安全的(在 IE8 或更低版本中不起作用),但 document.documentElement.scrollTop
是可以的。
https://dev59.com/l2Qn5IYBdhLWcg3wt5Eg - tvanc$(window).scroll(function () {
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
alert('end of page');
}
});
-10表示用户需要到达页面末尾多远才能执行函数。这使您可以根据需要调整行为。
>=
和 -10
对我很有帮助,那些愚蠢的错误,谢谢。 - dippas