Permalink
Browse files

Offset: account for scroll when calculating position

Fixes gh-1708
Close gh-1714
  • Loading branch information...
Richard McDaniel authored and timmywil committed Oct 16, 2014
1 parent c252c5f commit 2d715940b9b6fdeed005cd006c8bf63951cf7fb2
Showing with 17 additions and 3 deletions.
  1. +5 −2 src/offset.js
  2. +12 −1 test/unit/offset.js
View
@@ -131,8 +131,11 @@ jQuery.fn.extend({
}
// Add offsetParent borders
- parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
- parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
+ // Subtract offsetParent scroll positions
+ parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true ) -
+ offsetParent.scrollTop();
+ parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true ) -
+ offsetParent.scrollLeft();
}
// Subtract parent offsets and element margins
View
@@ -407,7 +407,7 @@ testIframe("offset/table", "table", function( $ ) {
});
testIframe("offset/scroll", "scroll", function( $, win ) {
- expect(24);
+ expect(28);
equal( $("#scroll-1").offset().top, 7, "jQuery('#scroll-1').offset().top" );
equal( $("#scroll-1").offset().left, 7, "jQuery('#scroll-1').offset().left" );
@@ -457,6 +457,17 @@ testIframe("offset/scroll", "scroll", function( $, win ) {
notEqual( $().scrollLeft(null), null, "jQuery().scrollLeft(null) testing setter on empty jquery object" );
strictEqual( $().scrollTop(), null, "jQuery().scrollTop(100) testing setter on empty jquery object" );
strictEqual( $().scrollLeft(), null, "jQuery().scrollLeft(100) testing setter on empty jquery object" );
+
+ // Tests position after parent scrolling (#15239)
+ $("#scroll-1").scrollTop(0);
+ $("#scroll-1").scrollLeft(0);
+ equal( $("#scroll-1-1").position().top, 6, "jQuery('#scroll-1-1').position().top unaffected by parent scrolling" );
+ equal( $("#scroll-1-1").position().left, 6, "jQuery('#scroll-1-1').position().left unaffected by parent scrolling" );
+
+ $("#scroll-1").scrollTop(5);
+ $("#scroll-1").scrollLeft(5);
+ equal( $("#scroll-1-1").position().top, 6, "jQuery('#scroll-1-1').position().top unaffected by parent scrolling" );
+ equal( $("#scroll-1-1").position().left, 6, "jQuery('#scroll-1-1').position().left unaffected by parent scrolling" );
});
testIframe("offset/body", "body", function( $ ) {

0 comments on commit 2d71594

Please sign in to comment.