offset() method in jQuery

offset() in jQuery

Get or Set value of the current coordinates of the first element in the set of matched elements, relative to the document.

offset() returns an object containing the properties of top and left.

Syntax 1 - Get coordinates

$(selector).offset()

Syntax 2 - Set coordinates

$(selector).offset(coordinates)

  • coordinates - An object containing the properties of top and left, which are numbers indicating the new top and left coordinates for the elements.
Syntax 3

$(selector).offset(function(index,currentCoordinates))

  • index - index position of the element in the set.
  • currentCoordinates - object with the new top and left properties.

Example 1 - Get the offset coordinates value

Try Yourself

Example 2 - set the offset coordinates value

Try Yourself

Example 3 - get & set offset value using function

Try Yourself

Example - Differennce between offset() and position()

Try Yourself