Given a div on a page. how to detect when the div is scrolled to the position where it is at the bottom of the browser window... flush with the bottom of the browser window?
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.
Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.
I don't think the above answer would work, since offset().top is the space between the div and the top of the document, and is not variable. This worked for me:
var a = $("#mydiv").offset().top;
var b = $("#mydiv").height();
var c = $(window).height();
var d = $(window).scrollTop();
if ((c+d)>(a+b)) {
//bottom of #mydiv has just become visible
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With