滚动到页面末尾时使用Jquery发出警报
有没有一种方法可以使用Jquery找到页面结尾,这样就可以显示一条简单的消息,说明您已到达页面的结尾。可能需要调整以适应浏览器,但类似的操作应该可以:滚动到页面末尾时使用Jquery发出警报,jquery,browser-scrollbars,Jquery,Browser Scrollbars,有没有一种方法可以使用Jquery找到页面结尾,这样就可以显示一条简单的消息,说明您已到达页面的结尾。可能需要调整以适应浏览器,但类似的操作应该可以: $(document).scroll(function() { var $body = $('body'); if (($body.get(0).scrollHeight - $body.scrollTop) == $body.height()) { // display your message
$(document).scroll(function()
{
var $body = $('body');
if (($body.get(0).scrollHeight - $body.scrollTop) == $body.height())
{
// display your message
}
});
这将起作用,我在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');
}
});
$(窗口)。滚动(函数()
{
如果(document.body.scrollHeight-$(this).scrollTop():
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.");
}
});
当用户滚动到页面底部时,在“完成!滚动到页面顶部”链接中消失
参考文献:
如果上述解决方案不起作用,请检查您是否正确设置了文档类型:
<!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);
}
});
调试注意事项:我在返回页面顶部时使用(?)获得警报
jquery-1.10.2.js。加载了jquery-1.6.4.min.js,一切正常。可能的重复对我来说不起作用。我建议大家接受这个问题的答案:@Ryan-JSFIDLE不适合你吗?你使用的是什么浏览器?或者,你可以在第一次成功执行后删除事件侦听器(console.log)