New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.position()
and .offsetParent()
return incorrect value
#3107
Comments
This behaviour is already defined in spec - https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetparent, we deviate from it though - Lines 162 to 166 in 95c7ab6
There is a lot of issues with |
If jQuery doesn't behave as with CSSOM specification, I think that |
.position()
method should return a position of element relative to a closest ancestor element that is positioned.Therefore, for example, if CSS properties
top/left
of the element that hasposition:absolute
are set to that returned position, the element should not move.But when the ancestor element that is positioned does not exist (i.e. all ancestor elements have
position:static
, this is default), a position relative to<html>
element incorrectly is returned.For example:
https://jsfiddle.net/fqbgy5pq/
Result:
Note:
2px
was lost by this issue, and more2px
was lost by another issue of.offset()
.When fixed
.offset()
is used:Result:
.offsetParent()
method that is called by.position()
method returns<html>
when ancestor element that is positioned is not found. Then,.position()
mistakes.jquery/src/offset.js
Line 180 in 305f193
I think that
.offsetParent()
should returndocument
ornull
in this case.document
means "base of coordinates".null
means "ancestor element is not found".Also,
.position()
method should return coordinates relative to the document in this case.And also,
.position()
method returns incorrect coordinates when<html>
hasposition:non-static
(i.e. the ancestor element is really<html>
).For example:
https://jsfiddle.net/wczL6ae5/
Result:
Note:
1px
was added by this issue, and2px
was lost by another issue of.offset()
.When fixed
.offset()
is used:Result:
When
.offsetParent()
method returned<html>
,.position()
method ignores its offset.jquery/src/offset.js
Line 144 in 305f193
Therefore, if the
<html>
has offset (i.e.margin
), returned value is incorrect.Strange to say, only
border-width
is included in that calculation.jquery/src/offset.js
Line 150 in 305f193
I think that it might affect scripts that exist if the return value of
.offsetParent()
method is changed.At least,
.position()
should be fixed even if.offsetParent()
is not fixed. In this case,.position()
should not use.offsetParent()
, or it should check whetherposition
isstatic
when<html>
was returned.This issue affects
.offset()
method because setter of.offset()
uses.position()
.The text was updated successfully, but these errors were encountered: