Description
The .offset() method sets or returns the offset (position) for the selected elements, relative to the document.
Return Offset Coordinates
Returns the offset coordinates of the FIRST matched element.
This method returns an object with 2 properties, top and left, which represent the top and left positions in pixels
v1.2
This method has the form:
.offset()
Return Value
This form returns an Object.
Set Offset Coordinates
Sets the offset coordinates of ALL matched elements
v1.4
This method has the alternate form:
.offset(value)
Parameter | Description |
---|---|
value | (required) Specifies the top and left coordinates in pixels. Possible values:
|
Return Value
This form returns a jQuery object.
Set Offset Coordinates Using a Function
Using a function to set the offset coordinates of ALL matched elements
v1.4
This method has the alternate form:
.offset(function(index,oldoffset))
Parameter | Description |
---|---|
function(index, oldoffset) | Specifies a function that returns an object containing the top and left coordinates.
|
Return Value
This form returns a jQuery object.
Examples
Get the current offset position of a p element:
$("button").click(function(){
x=$("p").offset();
alert("Left offset: " + x.left + " Top offset: " + x.top);
});