我如何判断滚动条是否已到达帖子内容的末尾

how would I tell if the scroll bar has reached the end of post's content?

本文关键字:何判断 判断 是否 滚动条      更新时间:2023-09-26

标题几乎不言自明。那么我将如何使用jQuery或javascript来做到这一点呢?我知道要判断滚动条是否已到达窗口的末尾,它将是这样的。

$(window).scroll(function(){
   if  ($(window).scrollTop() == $(document).height() - $(window).height()){
alert('you have reached the bottom');
}
});

那么我如何判断它是否已到达特定部分然后发出警报呢?谢谢。

当然,这不是一个难以解决的问题,但请查看jQuery Waypoints...一个非常好的插件,可以满足您的要求。

他们网站上的一个简单示例:

$('.entry').waypoint(function() {
   alert('You have scrolled to an entry.');
});

优雅且易于阅读,插件非常小,<4kb。

没有插件的一种方法是这样。

// calculate where the bottom of the post is positioned
var postBottom = $(".post").offset().top + $(".post").height();
$(window).scroll(function(){
    // check if the bottom of the post is smaller than the window height
    if (postBottom < $(window).height()){
        alert('you have reached the bottom of the post');
    }
});