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" ) &&
if ( arguments.length ) {
return options === undefined ?
this :
- this.each( function( i ) {
+ this.each(function( i ) {
jQuery.offset.setOffset( this, options, i );
- } );
+ });
}
- var docElem, win, rect,
+ var docElem, win,
elem = this[ 0 ],
+ box = { top: 0, left: 0 },
doc = elem && elem.ownerDocument;
- if ( !elem ) {
+ if ( !doc ) {
return;
}
- // Support: IE<=11+
- // Running getBoundingClientRect on a
- // disconnected node in IE throws an error
- if ( !elem.getClientRects().length ) {
- return { top: 0, left: 0 };
- }
-
- rect = elem.getBoundingClientRect();
-
- // Make sure element is not hidden (display: none) or disconnected
- if ( rect.width || rect.height || elem.getClientRects().length ) {
- win = getWindow( doc );
- docElem = doc.documentElement;
+ docElem = doc.documentElement;
- return {
- top: rect.top + win.pageYOffset - docElem.clientTop,
- left: rect.left + win.pageXOffset - docElem.clientLeft
- };
+ // Make sure it's not a disconnected DOM node
+ if ( !jQuery.contains( docElem, elem ) ) {
+ return box;
}
- // Return zeros for disconnected and hidden elements (gh-2310)
- return rect;
+ box = elem.getBoundingClientRect();
+ win = getWindow( doc );
+ return {
+ top: box.top + win.pageYOffset - docElem.clientTop,
+ left: box.left + win.pageXOffset - docElem.clientLeft
+ };
},
position: function() {
assert.equal( result.left, 0, "Check left" );
});
-QUnit.test( "disconnected element", function( assert ) {
+QUnit.test( "disconnected node", function( assert ) {
assert.expect( 2 );
var result = jQuery( document.createElement( "div" ) ).offset();
// 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 disconnected elements returns zeros (gh-2310)" );
- assert.equal( result.left, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" );
+ equal( result.top, 0, "Check top" );
+ equal( result.left, 0, "Check left" );
} );
QUnit.test( "hidden (display: none) element", function( assert ) {