Detecting when user scrolls to bottom of div with jQuery

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

提交回复
热议问题