有没有办法使用 Jquery 找出页面结束,以便显示一条简单的消息,说明您已到达页面末尾。
问问题
25515 次
7 回答
18
How to tell when you're at the bottom of a page:
if ( document.documentElement.clientHeight +
$(document).scrollTop() >= document.body.offsetHeight )
{
// Display alert or whatever you want to do when you're
// at the bottom of the page.
alert("You're at the bottom of the page.");
}
Of course you want to fire the above whenever the user scrolls:
$(window).scroll(function() {
if ( document.documentElement.clientHeight +
$(document).scrollTop() >= document.body.offsetHeight )
{
// Display alert or whatever you want to do when you're
// at the bottom of the page.
alert("You're at the bottom of the page.");
}
});
Here is a jsFiddle example that fades in a "You're Done! Scroll to Top of Page" link when the user has scrolled to the bottom of the page.
References:
于 2010-09-27T00:45:28.957 回答
6
这将起作用,我在 IE 7、8、9、FF 3.6、Chrome 6 和 Opera 10.6 中对其进行了测试
$(window).scroll(function()
{
if (document.body.scrollHeight - $(this).scrollTop() <= $(this).height())
{
alert('end');
}
});
于 2010-09-26T20:49:06.067 回答
2
如果上述解决方案不起作用,请检查您是否正确设置了文档类型:
<!DOCTYPE HTML>
我花了一个小时才知道:)
于 2013-03-27T09:52:30.210 回答
1
为避免重复console.log('end of page')
,您需要创建一个 setTimeout,如下所示:
var doc = $(document), w = $(window), timer;
doc.on('scroll', function(){
if(doc.scrollTop() + w.height() >= doc.height()){
if(typeof timer !== 'undefined') clearTimeout(timer);
timer = setTimeout(function(){
console.log('end of page');
}, 50);
}
});
于 2014-01-15T19:07:19.940 回答
0
调试注意事项:我在使用 jquery-1.10.2.js 返回页面顶部时收到警报(?)。加载 jquery-1.6.4.min.js 一切都很好。
于 2014-04-25T16:13:01.947 回答
0
它可能需要调整以考虑浏览器,但应该这样做:
$(document).scroll(function()
{
var $body = $('body');
if (($body.get(0).scrollHeight - $body.scrollTop) == $body.height())
{
// display your message
}
});
于 2010-09-26T20:02:08.803 回答
0
<script>
$(document).ready(function(){
var a = 1;
//this function triggers whenever scroll is detected
window.addEventListener("scroll", function(){
//this condition is used to trigger alert only once
if(a == 1){
//this condition check whether user scrolled to the
//bottom of the page or not
if(($(document).height()-2) <=
scrollY+$(window).height() ){
alert('end of the page');
a = 0;
}
}
})
});
</script>
于 2021-09-22T10:39:26.857 回答