Detecting when user scrolls to bottom of div with jQuery

前端 未结 15 1680
一个人的身影
一个人的身影 2020-11-22 14:47

I have a div box (called flux) with a variable amount of content inside. This divbox has overflow set to auto.

Now, what I am trying to do, is, when the use scroll t

相关标签:
15条回答
  • 2020-11-22 15:22

    Guys this is the solution to the zoom issue, it works with all zoom levels, in case you need it:

    if ( Math.abs(elem.offset().top) + elem.height() + elem.offset().top >= elem.outerHeight() ) {
                        console.log("bottom");
                        // We're at the bottom!
                    }
                });
            }
    
    0 讨论(0)
  • 2020-11-22 15:26

    There are some properties/methods you can use:

    $().scrollTop()//how much has been scrolled
    $().innerHeight()// inner height of the element
    DOMElement.scrollHeight//height of the content of the element
    

    So you can take the sum of the first two properties, and when it equals to the last property, you've reached the end:

    jQuery(function($) {
        $('#flux').on('scroll', function() {
            if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
                alert('end reached');
            }
        })
    });
    

    http://jsfiddle.net/doktormolle/w7X9N/

    Edit: I've updated 'bind' to 'on' as per:

    As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document.

    0 讨论(0)
  • 2020-11-22 15:26

    Just an additional note here as I found this when looking for a solution for a fixed div that I want to scroll. For my scenario I found that its preferable to take into account the padding on the div so I can hit the end exactly. So expanding on @Dr.Molle's answer I add the following

    $('#flux').bind('scroll', function() {
        var scrollPosition = $(this).scrollTop() + $(this).outerHeight();
        var divTotalHeight = $(this)[0].scrollHeight 
                              + parseInt($(this).css('padding-top'), 10) 
                              + parseInt($(this).css('padding-bottom'), 10)
                              + parseInt($(this).css('border-top-width'), 10)
                              + parseInt($(this).css('border-bottom-width'), 10);
    
        if( scrollPosition == divTotalHeight )
        {
          alert('end reached');
        }
      });
    

    That'll give you the precise location, including padding and borders

    0 讨论(0)
  • 2020-11-22 15:27

    Here's another version.

    The key code is function scroller() which takes input as the height of the div containing the scrolling section, using overflow:scroll. It approximates 5px from the top or 10px from the bottom as at the top or bottom. Otherwise it's too sensitive. It seems 10px is about the minimum. You'll see it adds 10 to the div height to get the bottom height. I assume 5px might work, I didn't test extensively. YMMV. scrollHeight returns the height of the inner scrolling area, not the displayed height of the div, which in this case is 400px.

    <?php
    
    $scrolling_area_height=400;
    echo '
    <script type="text/javascript">
              function scroller(ourheight) {
                var ourtop=document.getElementById(\'scrolling_area\').scrollTop;
                if (ourtop > (ourheight-\''.($scrolling_area_height+10).'\')) {
                    alert(\'at the bottom; ourtop:\'+ourtop+\' ourheight:\'+ourheight);
                }
                if (ourtop <= (5)) {
                    alert(\'Reached the top\');
                }
              }
    </script>
    
    <style type="text/css">
    .scroller { 
                display:block;
                float:left;
                top:10px;
                left:10px;
                height:'.$scrolling_area_height.';
                border:1px solid red;
                width:200px;
                overflow:scroll; 
            }
    </style>
    
    $content="your content here";
    
    <div id="scrolling_area" class="scroller">
    onscroll="var ourheight=document.getElementById(\'scrolling_area\').scrollHeight;
            scroller(ourheight);"
            >'.$content.'
    </div>';
    
    ?>
    
    0 讨论(0)
  • 2020-11-22 15:28

    this worked for me though

    $(window).scroll(function() {
      if ($(window).scrollTop() >= (($(document).height() - $(window).height()) - $('#divID').innerHeight())) {
        console.log('div reached');
      }
    });
    
    0 讨论(0)
  • 2020-11-22 15:29

    Though this was asked almost 6 years, ago still hot topic in UX design, here is demo snippet if any newbie wanted to use

    $(function() {
    
      /* this is only for demonstration purpose */
      var t = $('.posts').html(),
        c = 1,
        scroll_enabled = true;
    
      function load_ajax() {
    
        /* call ajax here... on success enable scroll  */
        $('.posts').append('<h4>' + (++c) + ' </h4>' + t);
    
        /*again enable loading on scroll... */
        scroll_enabled = true;
    
      }
    
    
      $(window).bind('scroll', function() {
        if (scroll_enabled) {
    
          /* if 90% scrolled */
        if($(window).scrollTop() >= ($('.posts').offset().top + $('.posts').outerHeight()-window.innerHeight)*0.9) {
    
            /* load ajax content */
            scroll_enabled = false;  
            load_ajax();
          }
    
        }
    
      });
    
    });
    h4 {
      color: red;
      font-size: 36px;
      background-color: yellow;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <div class="posts">
      Lorem ipsum dolor sit amet  Consectetuer augue nibh lacus at <br> Pretium Donec felis dolor penatibus <br> Phasellus consequat Vivamus dui lacinia <br> Ornare nonummy laoreet lacus Donec <br> Ut ut libero Curabitur id <br> Dui pretium hendrerit
      sapien Pellentesque <br> Lorem ipsum dolor sit amet <br> Consectetuer augue nibh lacus at <br> Pretium Donec felis dolor penatibus <br> Phasellus consequat Vivamus dui lacinia <br> Ornare nonummy laoreet lacus Donec <br> Ut ut libero Curabitur id <br>  Dui pretium hendrerit sapien Pellentesque <br> Lorem ipsum dolor sit amet <br> Consectetuer augue nibh lacus at <br> Pretium Donec felis dolor penatibus <br> Phasellus consequat Vivamus dui lacinia <br> Ornare nonummy laoreet lacus Donec <br> Ut ut
      libero Curabitur id <br> Dui pretium hendrerit sapien Pellentesque <br> Lorem ipsum dolor sit amet <br> Consectetuer augue nibh lacus at <br> Pretium Donec felis dolor penatibus <br> Phasellus consequat Vivamus dui lacinia <br> Ornare nonummy laoreet
      lacus Donec <br> Ut ut libero Curabitur id <br> Dui pretium hendrerit sapien Pellentesque
    </div>

    0 讨论(0)
提交回复
热议问题