elem.style.position = "relative";
}
- curOffset = curElem.offset() || { top: 0, left: 0 };
+ curOffset = curElem.offset();
curCSSTop = jQuery.css( elem, "top" );
curCSSLeft = jQuery.css( elem, "left" );
calculatePosition = ( position === "absolute" || position === "fixed" ) &&
} );
}
- var docElem, win, rect,
+ var docElem, win,
+ box = { top: 0, left: 0 },
elem = this[ 0 ],
doc = elem && elem.ownerDocument;
return;
}
- doc = elem.ownerDocument;
docElem = doc.documentElement;
- // Support: IE<=11+
- // Running getBoundingClientRect on a
- // disconnected node in IE throws an error
- // Support: IE8 only
- // getClientRects() errors on disconnected elems
+ // Make sure it's not a disconnected DOM node
if ( !jQuery.contains( docElem, elem ) ) {
- return { top: 0, left: 0 };
+ return box;
}
- rect = elem.getBoundingClientRect();
-
- if ( rect.width || rect.height || elem.getClientRects().length ) {
- win = getWindow( doc );
-
- return {
- top: rect.top + ( win.pageYOffset || docElem.scrollTop ) -
- ( docElem.clientTop || 0 ),
- left: rect.left + ( win.pageXOffset || docElem.scrollLeft ) -
- ( docElem.clientLeft || 0 )
- };
- }
-
- // Return zeros for hidden elements
- return rect;
+ box = elem.getBoundingClientRect();
+ win = getWindow( doc );
+ return {
+ top: box.top + ( win.pageYOffset || docElem.scrollTop ) - ( docElem.clientTop || 0 ),
+ left: box.left + ( win.pageXOffset || docElem.scrollLeft ) - ( docElem.clientLeft || 0 )
+ };
},
position: function() {
assert.equal( result.left, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" );
} );
-QUnit.test( "hidden (display: none) element", function( assert ) {
- assert.expect( 2 );
-
- var node = jQuery( "<div style='display: none' />" ).appendTo( "#qunit-fixture" ),
- result = node.offset();
-
- node.remove();
-
- // These tests are solely for master/compat consistency
- // Retrieving offset on disconnected/hidden elements is not officially
- // valid input, but will return zeros for back-compat
- assert.equal( result.top, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" );
- assert.equal( result.left, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" );
-} );
-
testIframe( "offset/absolute", "absolute", function( $, iframe, document, assert ) {
assert.expect( 4 );