简体   繁体   English

检测可滚动DIV是否已到达底部

[英]Detecting if a scrollable DIV has reached the bottom

I have a <div> with overflow: scroll; 我有一个<div> overflow: scroll; applied, that I want to detect when it's being scrolled, and trigger just as it hits 5% of it's total height remaining. 应用时,我想检测它何时滚动,并在其达到剩余总高度的5%时触发。

JSBin - Currently I'm failing to trigger when it hits the bottom of the page. JSBin-当前,当它到达页面底部时,我无法触发。

<div style="height:50%;overflow:scroll;">
  <b style="height:5000px;display:block;"></b>
</div>


$('div').on('scroll', function(){

  if($(window).scrollTop() + $('div').height() == $('div').height()) {
    console.log("bottom of page");
  }

});

The window is not scrollable in your example, the div is. 在您的示例中,该窗口不可滚动,而div是可滚动的。 This results in the div being the element to look for when checking the scrollTop() function. 这导致div是检查scrollTop()函数时要查找的元素。 Also, the summation here is greater than your b element (which you need to check for) and not equal. 同样,这里的总和大于您的b元素(您需要检查)并且不相等。 So changing the lines as follows the code executes as you've expected: 因此,如下所述更改行,代码将按您期望的那样执行:

$('div').on('scroll', function(){
    if($("div").scrollTop() + $('div').height() > $('b').height()) {
        console.log("bottom of page");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM