jQuery offset() method
Example
Return
The offset coordinates of an element:
$
(
"
button
"
)
.
click
(
function
(
)
{
var
x
=$
(
"
p
"
)
.
offset
(
)
;
alert
(
"
Top:
"
x
.
top
"
Left:
"
x
.
left
)
;
}
)
;
Try it »
Definition and Usage
The offset() method sets or returns the offset coordinates of the selected elements relative to the document.
When used to return the offset:
This method returns the offset coordinates of the first matched element. It returns an object with two properties (the top and left positions in pixels).
When used to set the offset:
This method sets the offset coordinates of all matched elements.
Syntax
Return the offset coordinates:
$(
selector
).offset()
Set the offset coordinates:
$(
selector
).offset({top:
value
,left:
value
})
Set the offset coordinates using a function:
$(
selector
).offset(function
(index,currentoffset)
)
| Parameters | Description |
|---|---|
| {top:value,left:value} | Required when setting the offset. Specifies the top and left coordinates in pixels. |
可能的值:
- index - Returns the index position of the element in the set.
- currentoffset - Returns the current coordinates of the selected elements.
More examples
Set the offset coordinates
How to set an element's offset coordinates.
Set offset coordinates using a function
Set an element's offset coordinates using a function.
Set offset coordinates for an element using an object
How to set an element's offset coordinates using a new object.
Set an element's offset coordinates using another element's offset coordinates
How to set an element's offset coordinates using another element's offset coordinates.
jQuery HTML/CSS methods