Avoid counting scroll twice in offset
Author: mgolCreated Apr 8, 2024Updated May 4, 2026
LabelsOffset
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
Source: jquery/jquery