Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery offset() method

jQuery offset() method

jQuery HTML/CSS methods jQuery HTML/CSS methods

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.
 可能的值:     
  • A name/value pair, such as {top:100,left:100}
  • An object with top and left propertiesExample)
  • | | function(*index,currentoffset*) | Optional. Specifies a function that returns an object containing the top and left coordinates. |

    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 jQuery HTML/CSS methods