Skip to content

Avoid counting scroll twice in offset #5468

@mgol

Description

@mgol

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 #4861 (comment)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions