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.
Get the current offset of the first matched element, in pixels, relative to the document.
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?