jQuery in Action
290
CHAPTER 9
Prominent, powerful, and practical plugins
relation to the window origin or to its offset parent? Other factors can also be
involved. For example, do we want dimensions such as border accounted for?
involved. For example, do we want dimensions such as border accounted for?
The Dimensions Plugin handles all of that for us, starting with obtaining the
offset parent.
When we want to obtain the relative position of an element from its offset parent,
we can use the
we can use the
position()
command.
This command is useful for when we want to reposition an element in relation to
its current location, but sometimes we want to know the position of an element in
relation to the
its current location, but sometimes we want to know the position of an element in
relation to the
<body>
element (regardless of what its offset parent is) and to have
a little more control over how the calculation is made. For those times, the
Dimensions Plugin provides the
Dimensions Plugin provides the
offset()
command.
Command syntax: offsetParent
offsetParent
Returns the offset parent (positioning context) for the first element in the wrapped set. This
is the closest ancestor with a position value of relative or absolute, or the <body> ele-
ment if no such ancestor is found. This method should only be applied to visible elements.
is the closest ancestor with a position value of relative or absolute, or the <body> ele-
ment if no such ancestor is found. This method should only be applied to visible elements.
Parameters
none
Returns
The offset parent element.
The offset parent element.
Command syntax: position
position()
Returns the position values (top and left) of the first element in the wrapped set relative to
its offset parent.
its offset parent.
Parameters
none
Returns
An object with two properties, top and left, containing the position values of the element
relative to its positioning context (offset parent).
An object with two properties, top and left, containing the position values of the element
relative to its positioning context (offset parent).