.offset()Returns: Object
Description: Get the current coordinates of the first element in the set of matched elements, relative to the document.
version added: 1.2.offset()
- This method does not accept any arguments.
The .offset()
method allows us to retrieve the current position of an element relative to the document. Contrast this with .position()
, which retrieves the current position relative to the offset parent. When positioning a new element on top of an existing one for global manipulation (in particular, for implementing drag-and-drop), .offset()
is the more useful.
.offset()
returns an object containing the properties top
and left
.
Note: jQuery does not support getting the offset coordinates of hidden elements or accounting for borders, margins, or padding set on the body element.
While it is possible to get the coordinates of elements with
visibility:hidden
set,display:none
is excluded from the rendering tree and thus has a position that is undefined.
Examples:
Example: Access the offset of the second paragraph:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Example: Click to see the offset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|