How can I determine if a div is scrolled to the bottom?

后端 未结 8 729
难免孤独
难免孤独 2020-12-12 20:18

How do I determine, without using jQuery or any other JavaScript library, if a div with a vertical scrollbar is scrolled all the way to the bottom?

My question is no

相关标签:
8条回答
  • 2020-12-12 20:48

    Well, I asked myself the same, and I found this way, here I put an example using an event:

    let scrollableElement = document.querySelector(".elementScrollable")
    
    scrollableElement.addEventListener("scroll",function(event){
      if(event.target.scrollTop === event.target.scrollTopMax){
            console.log("The scroll arrived at bottom")
      }
    })
    
    0 讨论(0)
  • 2020-12-12 20:53

    The solution i found on https://www.geeksforgeeks.org/how-to-detect-when-user-scrolls-to-the-bottom-of-a-div/ Worked for me, hope it will work for you too.

    $(window).on('scroll', function() { 
        if ($(window).scrollTop() >= $( 
            '.div').offset().top + $('.div'). 
            outerHeight() - window.innerHeight) { 
                alert('You reached the end of the DIV'); 
            } 
    });
    
    0 讨论(0)
提交回复
热议问题