Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the position of bottom of a div with jquery

Tags:

jquery

I have a div and want to find the bottom position. I can find the top position of the Div like this, but how do I find the bottom position?

var top = $('#bottom').position().top; return top; 
like image 975
lagwagon223 Avatar asked Oct 05 '12 15:10

lagwagon223


People also ask

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.

How do I find the offset on my bottom?

The offset bottom is calculating by using this statement “var bottom = el. offsetTop + $(el). outerHeight();” and calculated value is displaying.

How do you find XY coordinates of a div?

If the element is in the main document you can get the DIV's coordinates with... var X=window. getComputedStyle(abc,null). getPropertyValue('left'); var Y=window.


1 Answers

Add the outerheight to the top and you have the bottom, relative to the parent element:

var $el = $('#bottom');  //record the elem so you don't crawl the DOM everytime   var bottom = $el.position().top + $el.outerHeight(true); // passing "true" will also include the top and bottom margin 

With absolutely positioned elements or when positioning relative to the document, you will need to instead evaluate using offset:

var bottom = $el.offset().top + $el.outerHeight(true); 

As pointed out by trnelson this does not work 100% of the time. To use this method for positioned elements, you also must account for offset. For an example see the following code.

var bottom = $el.position().top + $el.offset().top + $el.outerHeight(true); 
like image 94
user1026361 Avatar answered Sep 24 '22 09:09

user1026361