在滚动条到达底部之前做一些100px的操作

Do something 100px before the scrollbar reaches bottom

本文关键字:100px 操作 滚动条 底部      更新时间:2023-09-26

我有下面的Javascript,当滚动条到达页面底部时,警报会按预期显示。

然而,我希望这种情况在到达底部之前发生100像素。我该怎么做?

$(window).scroll(function(){
  if($(window).scrollTop() == $(document).height() - $(window).height() ){
    alert("at bottom");
  }
}
$(window).scroll(function(){
  if($(window).scrollTop() + 100 > $(document).height() - $(window).height() ){
    alert("at bottom");
  }
});

使用>而不是==,因为滚动事件会偶尔触发,所以当存在精确匹配时,您可以滚动多次超过该值而不会触发事件。