Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a DIV is scrolled all the way to the bottom with jQuery

I have a div with overflow:scroll.

I want to know if it's currently scrolled all the way down. How, using JQuery?

This one doesn't work: How can I determine if a div is scrolled to the bottom?

like image 620
TIMEX Avatar asked Apr 29 '11 05:04

TIMEX


People also ask

How do I check if a div is scrolled to the bottom?

To detect when a user scrolls to bottom of div with React, we can check if the sum of the scrollTop and clientHeight properties of a scrollable element is equal to the scrollHeight property of the same element. We call the useRef hook to create a ref and we assign the returned ref to the inner div, which is scrollable.

How can check scroll to bottom in jQuery?

scrollTop() + $(window). height() == $(document). height()) { alert("bottom!"); } }); You can test it here, this takes the top scroll of the window, so how much it's scrolled down, adds the height of the visible window and checks if that equals the height of the overall content ( document ).

How do you find end of scroll?

To detect scroll end with JavaScript, we can listen to the scroll event. Then in the event listener, we check if offsetHeight + scrollTop is bigger than or equal to scrollHeight . We get the div with document. querySelector .


2 Answers

Here is the correct solution (jsfiddle). A brief look at the code:

$(document).ready(function () {     $('div').on('scroll', chk_scroll); });  function chk_scroll(e) {     var elem = $(e.currentTarget);     if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight()) {         console.log("bottom");     } } 

See this for more info.

like image 113
samccone Avatar answered Sep 20 '22 07:09

samccone


function isScrolledToBottom(el) {     var $el = $(el);     return el.scrollHeight - $el.scrollTop() - $el.outerHeight() < 1; } 

This is variation of @samccone's answer that incorporates @HenrikChristensen's comment regarding subpixel measurements.

like image 44
Chris Martin Avatar answered Sep 21 '22 07:09

Chris Martin