在滚动条到达底部之前执行100px的操作

时间:2011-06-02 19:46:57

标签: javascript jquery

我有以下Javascript,当滚动条点击页面底部时,警报会显示出来。

但是,我希望在到达底部之前发生100像素。我该怎么办?

$(window).scroll(function(){
  if($(window).scrollTop() == $(document).height() - $(window).height() ){

    alert("at bottom");

  }
}

1 个答案:

答案 0 :(得分:9)

$(window).scroll(function(){
  if($(window).scrollTop() + 100 > $(document).height() - $(window).height() ){

    alert("at bottom");

  }
});

使用>代替==因为滚动事件偶尔会触发,因此您可以多次滚动该值,而不会在精确匹配时触发事件。