Detecting when user scrolls to bottom of div with jQuery

前端 未结 15 1708
一个人的身影
一个人的身影 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:10

    I have crafted this piece of code that worked for me to detect when I scroll to the end of an element!

    let element = $('.element');
    
    if ($(document).scrollTop() > element.offset().top + element.height()) {
    
         /// do something ///
    }
    
    0 讨论(0)
  • 2020-11-22 15:12

    In simple DOM usage you can check the condition

    element.scrollTop + element.clientHeight == element.scrollHeight
    

    if true then you have reached the end.

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

    Though the question was asked 5.5 years ago, still it is more than relevant in today's UI/UX context. And I would like to add my two cents.

    var element = document.getElementById('flux');
    if (element.scrollHeight - element.scrollTop === element.clientHeight)
        {
            // element is at the end of its scroll, load more content
        }
    

    Source: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Determine_if_an_element_has_been_totally_scrolled

    Some elements won't allow you to scroll the full height of the element. In those cases you can use:

    var element = docuement.getElementById('flux');
    if (element.offsetHeight + element.scrollTop === element.scrollHeight) {
      // element is at the end of its scroll, load more content
    }
    
    0 讨论(0)
  • 2020-11-22 15:14
    $(window).on("scroll", function() {
        //get height of the (browser) window aka viewport
        var scrollHeight = $(document).height();
        // get height of the document 
        var scrollPosition = $(window).height() + $(window).scrollTop();
        if ((scrollHeight - scrollPosition) / scrollHeight === 0) {
            // code to run when scroll to bottom of the page
        }
    });
    

    This is the code on github.

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

    If anyone gets scrollHeight as undefined, then select elements' 1st subelement: mob_top_menu[0].scrollHeight

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

    If you are not using Math.round() function the solution suggested by Dr.Molle will not work in some cases when a browser window has a zoom.

    For example $(this).scrollTop() + $(this).innerHeight() = 600

    $(this)[0].scrollHeight yields = 599.99998

    600 >= 599.99998 fails.

    Here is the correct code:

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

    You may also add some extra margin pixels if you do not need a strict condition

    var margin = 4
    
    jQuery(function($) {
        $('#flux').on('scroll', function() {
            if(Math.round($(this).scrollTop() + $(this).innerHeight(), 10) >= Math.round($(this)[0].scrollHeight, 10) - margin) {
                alert('end reached');
            }
        })
    });
    
    0 讨论(0)
提交回复
热议问题