滚动到页面末尾时使用Jquery发出警报
问题描述:
有没有一种方法可以使用Jquery找出页面结尾,因此可以显示一条简单消息,表明您已到达页面结尾.
Is there a way to find out page end using Jquery, so that a simple message can be displayed saying you have reached end of the page.
答
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.");
}
});
这是一个jsFiddle示例 用户滚动到页面底部时,完成!滚动到页面顶部"链接.
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.
参考:
-
.scroll()
-
.scrollTop()
-
offsetHeight
clientHeight