Jquery gets the absolute and relative positions of HTML elements

  • 2020-03-30 03:24:56
  • OfStack

Absolute position coordinates:


$("#elem").offset().top
$("#elem").offset().left

Position coordinates relative to the parent element:


$("#elem").position().top
$("#elem").position().left

The other:
Static: the default positioning method.
Relative position: on the static basis, relative position of the original element changes by setting top,bottom,left,right.
Absolute: relative to the parent element. If there is a relative in the parent element, the position of the element is calculated. The reference for absolute positioning is the last parent element (static does not count). Absolute positioning causes the element to be removed from the document stream.
Fixed: the reference to fixed location is always the current document. With fixed positioning, it's easy to have a div positioned on the top left and top right of the browser document.


Related articles: