What is the difference between position() and offset() in jQuery ?
Both the jQueryUI method the returns the object which contains integer co-ordinate properties of the top and left positions. The positions of top and left coordinates are returned in pixels. Both functions are applied only on visible elements, not on hidden elements.
Example: The example gives top and left co-ordinates of the box which contains the text.
- Before clicking any button:
- After clicking the button:
Difference between offset() and position() Method:
|offset() Method||position() Method|
|The offset() method in jQuery returns the first found position of HTML element with respect to the document.||The position() method in jQuery returns the current position of HTML element with respect to its offset parent.|
|The jQuery UI offset() is relative to the document.||The jQuery UI position() is relative to the parent element.|
|When you want to position a new element on top of another one which is already existing, its better to use the jQuery offset() method.||When you want to position a new element near another DOM element within the same container, its better to use jQuery position() method.|
|The offset() method is mostly used in drag-and-drop functions.||The position() method is used to position an element relative to document, window or other elements like mouse or cursor.|