Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - detecting if the bottom of the div is touching the bottom of the browser window?

Tags:

jquery

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?

like image 765
AnApprentice Avatar asked Jan 11 '11 03:01

AnApprentice


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 do you get to the bottom of a div?

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.


1 Answers

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
}
like image 137
Keith Collins Avatar answered Sep 23 '22 23:09

Keith Collins