Javatpoint Logo

91-9990449935

 0120-4256464

jQuery offset()

The jQuery offset() method is used to get the current offset of the first matched element.

It provides two methods: to set or return the offset co-ordinates for the selected elements, relative to the document.

  • To return the offset: When this method is used to return the offset, it returns the offset co-ordinates of the FIRST matched element. It specifies the object's two properties: the top and left positions in pixels.
  • To set the offset: When this method is used to set the offset, it sets the offset co-ordinates of ALL matched elements.

Syntax:

To RETURN the offset co-ordinates:

To SET the offset co-ordinates:

To SET offset co-ordinates using a function:

Parameters of jQuery offset method

Parameter Description
{top:value,left:value} It is a mandatory parameter while setting the offset. It specifies the top and left co-ordinates in pixels.
Function (index,currentoffset): It is an optional parameter. It specifies a function that returns an object containing the top and left coordinates.
  • Index: It returns the index position of the element in the set.
  • Currentoffset:It returns the current coordinates of the selected element.

Example of jQuery offset() method

Let's take an example to demonstrate the jQuery offset() method.

Test it Now

jQuery offset() example 2

Test it Now
Next TopicjQuery position()