Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using jQuery to get absolute or relative position position of a div

I'll try to explain what i'm trying to get. I've a div with position:fixed (at the bottom of a page) into that div, there is some other divs. What i want is to show another div (with absolute position) aligned to the right with those divs... in other words, i wan't to know the left position of the divs...

like image 663
fidoboy Avatar asked Sep 03 '09 04:09

fidoboy


People also ask

Can a div be position absolute and relative?

Clearly, nothing can be absolute and relative at the same time. Either you want the element to position itself based on another ancestor's position or based on the page flow.

How do I set relative position in jQuery?

jQuery position() MethodThe position() method returns the position (relative to its parent element) of the first matched element. This method returns an object with 2 properties; the top and left positions in pixels.

How do I position a div relative to another?

First set position of the parent DIV to relative (specifying the offset, i.e. left , top etc. is not necessary) and then apply position: absolute to the child DIV with the offset you want. It's simple and should do the trick well.

Should I use position absolute or relative?

If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document. Position Absolute: When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go.


2 Answers

Relative to the document:

$('#ID').offset().left

Relative to its offset parent:

$('#ID').position().left
like image 84
Eric Avatar answered Sep 21 '22 02:09

Eric


$('#ID').position().left
like image 21
ChaosPandion Avatar answered Sep 21 '22 02:09

ChaosPandion