jQuery offset()
The jQuery offset()
method is used to retrieve or set the current position of an element relative to the document. It returns an object containing the top and left positions of the element. This method does not affect the position of the element, but merely retrieves its position.
Syntax
Here is the syntax for using the offset()
method:
$(selector).offset()
To set the position of an element using offset()
, you can also pass an object with top
and left
properties:
$(selector).offset({ top: value, left: value })
Use
The offset()
method is commonly used to position elements dynamically on a webpage. By retrieving the current position of an element, you can use the returned values to calculate the new position of that element based on user interactions, screen sizes, or other factors.
Example
Here is an example of using offset()
to retrieve the position of an element: