Detecting when user scrolls to bottom of div with jQuery

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

提交回复
热议问题