【问题标题】:Do something 100px before the scrollbar reaches bottom在滚动条到达底部之前执行 100px 的操作
【发布时间】:2016-03-31 21:37:04
【问题描述】:

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

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

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

    alert("at bottom");

  }
}

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:
    $(window).scroll(function(){
      if($(window).scrollTop() + 100 > $(document).height() - $(window).height() ){
    
        alert("at bottom");
    
      }
    });
    

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

    【讨论】:

    • 此解决方案在 IE 中多次触发。
    • @usefulBee 是的,我让开发人员决定是否已经采取了计划的行动。此解决方案只是要弄清楚您何时处于目标滚动位置。
    猜你喜欢
    • 2016-03-09
    • 2014-12-12
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 2015-05-30
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多