Easy Tutorial
❮ Event Resize Jq Sel Target ❯

jQuery offset() Method

jQuery HTML/CSS Methods

Example

Return the offset coordinates of a <p> element:

$("button").click(function(){
    var x=$("p").offset();
    alert("Top: " + x.top + " Left: " + x.left);
});

Definition and Usage

The offset() method sets or returns the offset coordinates of the selected element relative to the document.

When used to return offsets:

When used to set offsets:


Syntax

Return offset coordinates:

Set offset coordinates:

Set offset coordinates using a function:

Parameter Description
{top: value, left: value} Required when setting offsets. Specifies the top and left coordinates in pixels. <br> <br>Possible values: name/value pairs, such as {top:100, left:100}<br> An object with top and left properties
function(index, currentoffset) Optional. Specifies a function that returns an object containing the top and left coordinates. index - Returns the index position of the element in the set.<br> <br>currentoffset - Returns the current coordinates of the selected element.

More Examples

Set Offset Coordinates

Set Offset Coordinates Using a Function

Set Offset Coordinates Using an Object

Set Offset Coordinates Using Another Element's Offsets


jQuery HTML/CSS Methods

❮ Event Resize Jq Sel Target ❯