Skip to content

Commit 0654711

Browse files
Richard McDanieltimmywil
authored andcommitted
Offset: account for scroll when calculating position
Fixes gh-1708 Close gh-1714
1 parent b35bea1 commit 0654711

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
@@ -137,8 +137,11 @@ jQuery.fn.extend({
137137
}
138138

139139
// Add offsetParent borders
140-
parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
141-
parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
140+
// Subtract offsetParent scroll positions
141+
parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true ) -
142+
offsetParent.scrollTop();
143+
parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true ) -
144+
offsetParent.scrollLeft();
142145
}
143146

144147
// Subtract parent offsets and element margins

test/unit/offset.js

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

414414
testIframe("offset/scroll", "scroll", function( $, win ) {
415-
expect(24);
415+
expect(28);
416416

417417
// If we're going to bastardize the tests, let's just DO it
418418
var ie = /msie 8/i.test( navigator.userAgent );
@@ -473,6 +473,17 @@ testIframe("offset/scroll", "scroll", function( $, win ) {
473473
notEqual( $().scrollLeft(null), null, "jQuery().scrollLeft(null) testing setter on empty jquery object" );
474474
strictEqual( $().scrollTop(), null, "jQuery().scrollTop(100) testing setter on empty jquery object" );
475475
strictEqual( $().scrollLeft(), null, "jQuery().scrollLeft(100) testing setter on empty jquery object" );
476+
477+
// Tests position after parent scrolling (#15239)
478+
$("#scroll-1").scrollTop(0);
479+
$("#scroll-1").scrollLeft(0);
480+
equal( $("#scroll-1-1").position().top, 6, "jQuery('#scroll-1-1').position().top unaffected by parent scrolling" );
481+
equal( $("#scroll-1-1").position().left, 6, "jQuery('#scroll-1-1').position().left unaffected by parent scrolling" );
482+
483+
$("#scroll-1").scrollTop(5);
484+
$("#scroll-1").scrollLeft(5);
485+
equal( $("#scroll-1-1").position().top, 6, "jQuery('#scroll-1-1').position().top unaffected by parent scrolling" );
486+
equal( $("#scroll-1-1").position().left, 6, "jQuery('#scroll-1-1').position().left unaffected by parent scrolling" );
476487
});
477488

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

0 commit comments

Comments
 (0)