Skip to content

Commit 556eaf4

Browse files
authored
Offset: Increase search depth when finding the 'real' offset parent
Changes: * Increase search depth when finding for the real offset parent * Ignore offset for statically positioned offset parent * Add tests for the position of an element in a table Closes gh-4861
1 parent df1df95 commit 556eaf4

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/offset.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,13 @@ jQuery.fn.extend( {
121121
doc = elem.ownerDocument;
122122
offsetParent = elem.offsetParent || doc.documentElement;
123123
while ( offsetParent &&
124-
( offsetParent === doc.body || offsetParent === doc.documentElement ) &&
124+
offsetParent !== doc.documentElement &&
125125
jQuery.css( offsetParent, "position" ) === "static" ) {
126126

127-
offsetParent = offsetParent.parentNode;
127+
offsetParent = offsetParent.offsetParent || doc.documentElement;
128128
}
129-
if ( offsetParent && offsetParent !== elem && offsetParent.nodeType === 1 ) {
129+
if ( offsetParent && offsetParent !== elem && offsetParent.nodeType === 1 &&
130+
jQuery.css( offsetParent, "position" ) !== "static" ) {
130131

131132
// Incorporate borders into its offset, since they are outside its content origin
132133
parentOffset = jQuery( offsetParent ).offset();

test/unit/offset.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,16 @@ testIframe( "fixed", "offset/fixed.html", function( assert, $, window ) {
436436
} );
437437

438438
testIframe( "table", "offset/table.html", function( assert, $ ) {
439-
assert.expect( 4 );
439+
assert.expect( 6 );
440440

441441
assert.equal( $( "#table-1" ).offset().top, 6, "jQuery('#table-1').offset().top" );
442442
assert.equal( $( "#table-1" ).offset().left, 6, "jQuery('#table-1').offset().left" );
443443

444444
assert.equal( $( "#th-1" ).offset().top, 10, "jQuery('#th-1').offset().top" );
445445
assert.equal( $( "#th-1" ).offset().left, 10, "jQuery('#th-1').offset().left" );
446+
447+
assert.equal( $( "#th-1" ).position().top, 10, "jQuery('#th-1').position().top" );
448+
assert.equal( $( "#th-1" ).position().left, 10, "jQuery('#th-1').position().left" );
446449
} );
447450

448451
testIframe( "scroll", "offset/scroll.html", function( assert, $, win ) {

0 commit comments

Comments
 (0)