是否有一种使用Jquery找出页面末尾的方法,以便显示一个简单的消息,告诉用户已经到达页面底部。
是否有一种使用Jquery找出页面末尾的方法,以便显示一个简单的消息,告诉用户已经到达页面底部。
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.");
}
当然,您希望在用户滚动时触发上述操作:
$(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 示例,当用户滚动到页面底部时,会淡入一个“您已完成!返回页面顶部”的链接。
参考资料:
$(window).scroll(function()
{
if (document.body.scrollHeight - $(this).scrollTop() <= $(this).height())
{
alert('end');
}
});
<!DOCTYPE HTML>
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);
}
});
<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>
可能需要进行微调以适应不同的浏览器,但是类似这样的代码应该可以胜任:
$(document).scroll(function()
{
var $body = $('body');
if (($body.get(0).scrollHeight - $body.scrollTop) == $body.height())
{
// display your message
}
});