如何判断div是否滚动到底部?

时间:2009-05-18 03:19:06

标签: javascript scroll scrollbar

如果不使用jQuery或任何其他JavaScript库,如果将带有垂直滚动条的div一直滚动到底部,我如何确定?

我的问题不是如何滚动到底部。我知道该怎么做。我想确定div是否已经滚动到底部。

这不起作用:

if (objDiv.scrollTop == objDiv.scrollHeight) 

9 个答案:

答案 0 :(得分:72)

你使用scrollTop == scrollHeight非常接近。

scrollTop指的是滚动位置的顶部,即scrollHeight - offsetHeight

你的if语句应该如此(不要忘记使用三等于):

if( obj.scrollTop === (obj.scrollHeight - obj.offsetHeight))
{
}

编辑:纠正了我的回答,完全错了

答案 1 :(得分:6)

如果元素位于其滚动的末尾,则返回true,否则返回false。

element.scrollHeight - element.scrollTop === element.clientHeight

Mozilla Developer Network

答案 2 :(得分:5)

为了在考虑边框,水平滚动条和/或浮动像素计数等内容时获得正确的结果,您应该使用...

el.scrollHeight - el.scrollTop - el.clientHeight < 1

注意:如果要获得正确的结果,必须使用clientHeight而不是offsetHeight。仅当el没有边框或水平滚动条时,offsetHeight才会给出正确的结果

答案 3 :(得分:5)

这个派对的时间已经很晚了,但上述答案似乎都没有特别好......

  • 显示缩放适用于UHD显示的操作系统
  • 缩放/缩放应用于浏览器

为适应所有可能性,您需要对计算出的滚动位置进行舍入:

Math.ceil(element.scrollHeight - element.scrollTop) === element.clientHeight

答案 4 :(得分:1)

function difference(first,sec){
  return Math.abs(first-sec);
}

document.getElementById("myDIV").addEventListener("scroll", function() {
  var diff = difference((this.scrollTop+this.clientHeight),(this.scrollHeight));
   
   if(diff < 5) {
      alert('Scroll Ends Here');
    }
});
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
  height: 250px;
  width: 250px;
  overflow: none;
  overflow-y: auto;
}

#content {
  height: 800px;
  width: 2000px;
  background-color: coral;
}
</style>
</head>
<body>

<p>Scroll inside the div element to display the number of pixels the content of div is scrolled horizontally and vertically.</p>

<div id="myDIV">
  <div id="content">Scroll inside me!</div>
</div>

<p id="demo"></p>

</body>
</html>

没有 JQuery 或 Javascript 库,它运行良好。

答案 5 :(得分:0)

<!DOCTYPE HTML> 
<html> 
<head> 
    <title>JQuery | Detecting when user scrolls to bottom of div. 
    </title> 
</head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> 

<body style="text-align:center;" id="body"> 
    <h1 style="color:green;">  
            Data Center  
        </h1> 
    <p id="data_center" style="font-size: 17px; font-weight: bold;"></p> 
    <center> 
        <div class="div" style="width:200px; height:150px; overflow:auto;"> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
            <h1>Hello Friends</h1> 
        </div> 
    </center> 
    <script> 
        $('#data_center').text('Scroll till bottom to get alert!'); 
        jQuery(function($) { 
            $('.div').on('scroll', function() { 
                if ($(this).scrollTop() + $(this).innerHeight() >=  $(this)[0].scrollHeight) {                     
                    alert('End of DIV has reached!'); 
                } 
            }); 
        }); 
    </script> 
</body> 

</html> 

它对我有用,我希望这对您也有帮助。谢谢。

答案 6 :(得分:0)

好吧,我问自己也是一样,然后我发现了这种方式,在这里我举了一个使用事件的例子:

let scrollableElement = document.querySelector(".elementScrollable")

scrollableElement.addEventListener("scroll",function(event){
  if(event.target.scrollTop === event.target.scrollTopMax){
        console.log("The scroll arrived at bottom")
  }
})

答案 7 :(得分:0)

这个对我有用。略有不同。

if (list.scrollTop + list.clientHeight >= list.scrollHeight - 1) {
    return 'scrollOnTheBottom';
}

答案 8 :(得分:0)

我在https://www.geeksforgeeks.org/how-to-detect-when-user-scrolls-to-the-bottom-of-a-div/上找到的解决方案为我工作,希望它也对您有用。

$(window).on('scroll', function() { 
    if ($(window).scrollTop() >= $( 
        '.div').offset().top + $('.div'). 
        outerHeight() - window.innerHeight) { 
            alert('You reached the end of the DIV'); 
        } 
});
相关问题