在滚动条到达底部之前做一些 100px 的事情 [英] Do something 100px before the scrollbar reaches bottom

查看:22
本文介绍了在滚动条到达底部之前做一些 100px 的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the below Javascript, and the alert shows up as it is supposed to when the scrollbar hits the bottom of the page.

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

However, I would like this to happen 100 pixels before it reaches the bottom. How would I do that?

$(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");

  }
});

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

Using > instead of == because the scroll event fires sporadically, so you may scroll past that value a bunch of times without the event ever firing when there's a precise match.

这篇关于在滚动条到达底部之前做一些 100px 的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆