Get the absolute position of a div relative to the window

This question is quite similar to what I want I’m looking for, but my situation is different enough, I think, to merit a new question. I have several divs positioned absolutely inside of a parent div (position: relative). I want to get the position of the children divs relative to the window. The jquery offset() method doesn’t seem …

via Css Website development » Search Results » ajax:

Get the absolute position of a div relative to the window

This question is quite similar to what I want I’m looking for, but my situation is different enough, I think, to merit a new question.
I have several divs positioned absolutely inside of a parent div (position: relative). I want to get the position of the children divs relative to the window. The jquery offset() method doesn’t seem to work, because it gives me the offset from the parent div. Is there a way to get the absolute position of a div that has absolute position inside of a relative position div?
Sample html:

Child 1

Child 2

…………………………………

There’s the .position() function too, however (though they’re perpetually confusing to me) my understanding is backwards from what you’ve written: .offset() is relative to the document origin, while .position() is relative to the “offset parent”.
I think it’s the fact that the term “offset parent” contains the word “offset” that confuses me.
…………………………………

Have you looked at .offsetParent(), you can traverse up to the window and get the total offset.
See also http://www.sitepoint.com/forums/showthread.php?t=620402
…………………………………

I did a test with the html below. The value I get for offset is different than position. Is something else going on that changes where the parent div is positioned? If another element is getting added to the DOM after you check the position and offset values, then maybe you need to check position later. Maybe using inline style as opposed to using CSS classes makes a difference? (you will need to get json2.js from http://www.json.org/js.htm)



John

Joseph

Justin



…………………………………

Hi Use the position () function of jQuery library.
Create a Div called Stage and another called Content. On Stage Div write position absolute, top and left. In Div Content type position absolute, top and left too.
Ok?

For more info: Get the absolute position of a div relative to the window

Css Website development » Search Results » ajax

Get the absolute position of a div relative to the window

Share this post:

Related Posts

Leave a Comment