Skip to content

Commit 2d71594

Browse files
Richard McDanieltimmywil
Richard McDaniel
authored andcommitted
Offset: account for scroll when calculating position
Fixes jquerygh-1708 Close jquerygh-1714
1 parent c252c5f commit 2d71594

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/offset.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,11 @@ jQuery.fn.extend({
131131
}
132132

133133
// Add offsetParent borders
134-
parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
135-
parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
134+
// Subtract offsetParent scroll positions
135+
parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true ) -
136+
offsetParent.scrollTop();
137+
parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true ) -
138+
offsetParent.scrollLeft();
136139
}
137140

138141
// Subtract parent offsets and element margins

test/unit/offset.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ testIframe("offset/table", "table", function( $ ) {
407407
});
408408

409409
testIframe("offset/scroll", "scroll", function( $, win ) {
410-
expect(24);
410+
expect(28);
411411

412412
equal( $("#scroll-1").offset().top, 7, "jQuery('#scroll-1').offset().top" );
413413
equal( $("#scroll-1").offset().left, 7, "jQuery('#scroll-1').offset().left" );
@@ -457,6 +457,17 @@ testIframe("offset/scroll", "scroll", function( $, win ) {
457457
notEqual( $().scrollLeft(null), null, "jQuery().scrollLeft(null) testing setter on empty jquery object" );
458458
strictEqual( $().scrollTop(), null, "jQuery().scrollTop(100) testing setter on empty jquery object" );
459459
strictEqual( $().scrollLeft(), null, "jQuery().scrollLeft(100) testing setter on empty jquery object" );
460+
461+
// Tests position after parent scrolling (#15239)
462+
$("#scroll-1").scrollTop(0);
463+
$("#scroll-1").scrollLeft(0);
464+
equal( $("#scroll-1-1").position().top, 6, "jQuery('#scroll-1-1').position().top unaffected by parent scrolling" );
465+
equal( $("#scroll-1-1").position().left, 6, "jQuery('#scroll-1-1').position().left unaffected by parent scrolling" );
466+
467+
$("#scroll-1").scrollTop(5);
468+
$("#scroll-1").scrollLeft(5);
469+
equal( $("#scroll-1-1").position().top, 6, "jQuery('#scroll-1-1').position().top unaffected by parent scrolling" );
470+
equal( $("#scroll-1-1").position().left, 6, "jQuery('#scroll-1-1').position().left unaffected by parent scrolling" );
460471
});
461472

462473
testIframe("offset/body", "body", function( $ ) {

0 commit comments

Comments
 (0)