Skip to content

Commit

Permalink
Offset: account for scroll when calculating position
Browse files Browse the repository at this point in the history
Fixes gh-1708
Close gh-1714
  • Loading branch information
Richard McDaniel authored and timmywil committed May 12, 2015
1 parent c252c5f commit 2d71594
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/offset.js
Expand Up @@ -131,8 +131,11 @@ jQuery.fn.extend({
} }


// Add offsetParent borders // Add offsetParent borders
parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true ); // Subtract offsetParent scroll positions
parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true ); 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 // Subtract parent offsets and element margins
Expand Down
13 changes: 12 additions & 1 deletion test/unit/offset.js
Expand Up @@ -407,7 +407,7 @@ testIframe("offset/table", "table", function( $ ) {
}); });


testIframe("offset/scroll", "scroll", function( $, win ) { 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().top, 7, "jQuery('#scroll-1').offset().top" );
equal( $("#scroll-1").offset().left, 7, "jQuery('#scroll-1').offset().left" ); equal( $("#scroll-1").offset().left, 7, "jQuery('#scroll-1').offset().left" );
Expand Down Expand Up @@ -457,6 +457,17 @@ testIframe("offset/scroll", "scroll", function( $, win ) {
notEqual( $().scrollLeft(null), null, "jQuery().scrollLeft(null) testing setter on empty jquery object" ); 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( $().scrollTop(), null, "jQuery().scrollTop(100) testing setter on empty jquery object" );
strictEqual( $().scrollLeft(), null, "jQuery().scrollLeft(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( $ ) { testIframe("offset/body", "body", function( $ ) {
Expand Down

0 comments on commit 2d71594

Please sign in to comment.