Reputation: 73195
Consider the following diagram:
I would like to calculate the vertical offset of the child element within its immediate parent. Note that the child element has a margin (indicated by the lighter blue).
jQuery provides .position()
, but unfortunately the method calculates offset:
"...relative to the offset parent."
The parent element in this case does not have position
set to relative
and unfortunately this cannot be changed.
In spite of this, is there any way to get the child element's offset relative to its immediate parent?
Upvotes: 2
Views: 252
Reputation: 437376
You can do that with
$(this).offset().top - $(this).parent().offset().top
Upvotes: 2