
Recently I’ve been taking a look at returning coordinate (x, y) values in jQuery. At first I was a bit confused between the differences between two functions in jQuery. These two functions are offset() and position(). Given the jQuery documentation, the descriptions for these two functions could probably use a little more descriptive reference. Though I may just be playing that card in part of my own misunderstanding at first, though it wouldn’t be the first time the jQuery docs have confused me.
Respectively lets look at the descriptions for these two functions.
Offset()
Get the current offset of the first matched element, in pixels, relative to the document.
and
Postion()
Gets the top and left position of an element relative to its offset parent.
My main confusion between these two functions, was not entirely based around the description, but largely the examples provided within the documentation. Both function’s examples are identical except for the function name.
When applying the examples in an experimental plane (my testing server) within an element the difference is very clear. Offset by default returns the x and y coordinate relative to the document body, regardless if it is set within another element. Position by default returns the x and y coordinate relative to the element its positioned in. While certainly the offset description may be enough, the position functions description could possibly do without the ‘offset’ contained within it, or at least contain a bit more introspective.
Just my thoughts, what are yours?
Dude, seriously…you saved me. JQuery’s doc on this one made absolutely no sense based on the examples given. It could be my overall ignorance as well though.. But it definitely can be a explained a bit tighter. Perhaps you should suggest a revision… In any case, thanks again!
THX so much man. I needed this information!
Thank you. I was actually guessing that this was what jquery docs meant. You made it very clear now. Once again, thank you.
that means if i want to get the x,y coordinates with an absolute positioned parent offset never works correctly ?
thanks
That makes it a lot clear!! The jQuery docs sure had me in a fix!
Thanks
What’s funny is when you look up information on another site hoping to get a different explanation of .position() and .offset() they just quote jQuery’s docs… which completely defeats the purpose of trying to find a different explanation.