jquery / jquery

jQuery JavaScript Library
https://jquery.com
MIT License
58.93k stars 20.62k forks source link

Avoid counting scroll twice in offset #5468

Open mgol opened 1 month ago

mgol commented 1 month ago

While we're here making changes, we could also account for scrolling.

-             // Incorporate borders into its offset, since they are outside its content origin
+             // The origin against which we will be returning a relative position is the absolute offset
+             // of offsetParent, plus the top/left width of its borders (since they are outside offsetParent's
+             // content origin), minus its top/left scroll position (which has already affected element
+             // absolute offset and should not be counted twice)
              parentOffset = jQuery( offsetParent ).offset();
-             parentOffset.top += > jQuery.css( offsetParent, "borderTopWidth", true );
-             parentOffset.left > += jQuery.css( offsetParent, "borderLeftWidth", true );
+             parentOffset.top += jQuery.css( offsetParent, "borderTopWidth", true ) - offsetParent.scrollTop;
+             parentOffset.left += jQuery.css( offsetParent, "borderLeftWidth", true ) - offsetParent.scrollLeft;

_Originally posted by @gibson042 in https://github.com/jquery/jquery/pull/4861#discussion_r1537951644_