代码之家  ›  专栏  ›  技术社区  ›  Bjorn

如何确定div是否滚动到底?

  •  125
  • Bjorn  · 技术社区  · 17 年前

    if (objDiv.scrollTop == objDiv.scrollHeight) 
    
    8 回复  |  直到 16 年前
        1
  •  127
  •   SomeKittens    10 年前

    scrollTop == scrollHeight .

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

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

        2
  •  37
  •   Spencer    9 年前

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

        3
  •  15
  •   Matthew Layton    8 年前

    为了适应所有可能的情况,您需要将计算出的滚动位置四舍五入:

    Math.ceil(element.scrollHeight - element.scrollTop) === element.clientHeight
    
        4
  •  10
  •   Emmanuel Osimosu    10 年前

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

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

    Mozilla Developer Network

        5
  •  1
  •   Arthur A. Kennedy    4 年前

    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库,但它运行良好。

        6
  •  0
  •   Kamlesh Dev01    6 年前
    <!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> 
    

    我希望这对我有效,这也会对你有所帮助。谢谢。

        7
  •  0
  •   Aleks Grunwald    5 年前

    if (list.scrollTop + list.clientHeight >= list.scrollHeight - 1) {
        return 'scrollOnTheBottom';
    }
    
        8
  •  0
  •   Gleb Sabirzyanov Himanshu Patni    5 年前

    我找到的解决方案 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'); 
            } 
    });
    
    推荐文章