mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
170 lines
4.6 KiB
JavaScript
170 lines
4.6 KiB
JavaScript
jQuery.fn.offset = function( options ) {
|
|
if ( arguments.length ) {
|
|
return options === undefined ?
|
|
this :
|
|
this.each(function( i ) {
|
|
jQuery.offset.setOffset( this, options, i );
|
|
});
|
|
}
|
|
|
|
var docElem, win,
|
|
box = { top: 0, left: 0 },
|
|
elem = this[ 0 ],
|
|
doc = elem && elem.ownerDocument;
|
|
|
|
if ( !doc ) {
|
|
return;
|
|
}
|
|
|
|
docElem = doc.documentElement;
|
|
|
|
// Make sure it's not a disconnected DOM node
|
|
if ( !jQuery.contains( docElem, elem ) ) {
|
|
return box;
|
|
}
|
|
|
|
// If we don't have gBCR, just use 0,0 rather than error
|
|
// BlackBerry 5, iOS 3 (original iPhone)
|
|
if ( typeof elem.getBoundingClientRect !== core_strundefined ) {
|
|
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 )
|
|
};
|
|
};
|
|
|
|
jQuery.offset = {
|
|
|
|
setOffset: function( elem, options, i ) {
|
|
var position = jQuery.css( elem, "position" );
|
|
|
|
// set position first, in-case top/left are set even on static elem
|
|
if ( position === "static" ) {
|
|
elem.style.position = "relative";
|
|
}
|
|
|
|
var curElem = jQuery( elem ),
|
|
curOffset = curElem.offset(),
|
|
curCSSTop = jQuery.css( elem, "top" ),
|
|
curCSSLeft = jQuery.css( elem, "left" ),
|
|
calculatePosition = ( position === "absolute" || position === "fixed" ) && jQuery.inArray("auto", [curCSSTop, curCSSLeft]) > -1,
|
|
props = {}, curPosition = {}, curTop, curLeft;
|
|
|
|
// need to be able to calculate position if either top or left is auto and position is either absolute or fixed
|
|
if ( calculatePosition ) {
|
|
curPosition = curElem.position();
|
|
curTop = curPosition.top;
|
|
curLeft = curPosition.left;
|
|
} else {
|
|
curTop = parseFloat( curCSSTop ) || 0;
|
|
curLeft = parseFloat( curCSSLeft ) || 0;
|
|
}
|
|
|
|
if ( jQuery.isFunction( options ) ) {
|
|
options = options.call( elem, i, curOffset );
|
|
}
|
|
|
|
if ( options.top != null ) {
|
|
props.top = ( options.top - curOffset.top ) + curTop;
|
|
}
|
|
if ( options.left != null ) {
|
|
props.left = ( options.left - curOffset.left ) + curLeft;
|
|
}
|
|
|
|
if ( "using" in options ) {
|
|
options.using.call( elem, props );
|
|
} else {
|
|
curElem.css( props );
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
jQuery.fn.extend({
|
|
|
|
position: function() {
|
|
if ( !this[ 0 ] ) {
|
|
return;
|
|
}
|
|
|
|
var offsetParent, offset,
|
|
parentOffset = { top: 0, left: 0 },
|
|
elem = this[ 0 ];
|
|
|
|
// fixed elements are offset from window (parentOffset = {top:0, left: 0}, because it is it's only offset parent
|
|
if ( jQuery.css( elem, "position" ) === "fixed" ) {
|
|
// we assume that getBoundingClientRect is available when computed position is fixed
|
|
offset = elem.getBoundingClientRect();
|
|
} else {
|
|
// Get *real* offsetParent
|
|
offsetParent = this.offsetParent();
|
|
|
|
// Get correct offsets
|
|
offset = this.offset();
|
|
if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
|
|
parentOffset = offsetParent.offset();
|
|
}
|
|
|
|
// Add offsetParent borders
|
|
parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
|
|
parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
|
|
}
|
|
|
|
// Subtract parent offsets and element margins
|
|
// note: when an element has margin: auto the offsetLeft and marginLeft
|
|
// are the same in Safari causing offset.left to incorrectly be 0
|
|
return {
|
|
top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
|
|
left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true)
|
|
};
|
|
},
|
|
|
|
offsetParent: function() {
|
|
return this.map(function() {
|
|
var offsetParent = this.offsetParent || docElem;
|
|
while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" ) && jQuery.css( offsetParent, "position") === "static" ) ) {
|
|
offsetParent = offsetParent.offsetParent;
|
|
}
|
|
return offsetParent || docElem;
|
|
});
|
|
}
|
|
});
|
|
|
|
|
|
// Create scrollLeft and scrollTop methods
|
|
jQuery.each( {scrollLeft: "pageXOffset", scrollTop: "pageYOffset"}, function( method, prop ) {
|
|
var top = /Y/.test( prop );
|
|
|
|
jQuery.fn[ method ] = function( val ) {
|
|
return jQuery.access( this, function( elem, method, val ) {
|
|
var win = getWindow( elem );
|
|
|
|
if ( val === undefined ) {
|
|
return win ? (prop in win) ? win[ prop ] :
|
|
win.document.documentElement[ method ] :
|
|
elem[ method ];
|
|
}
|
|
|
|
if ( win ) {
|
|
win.scrollTo(
|
|
!top ? val : jQuery( win ).scrollLeft(),
|
|
top ? val : jQuery( win ).scrollTop()
|
|
);
|
|
|
|
} else {
|
|
elem[ method ] = val;
|
|
}
|
|
}, method, val, arguments.length, null );
|
|
};
|
|
});
|
|
|
|
function getWindow( elem ) {
|
|
return jQuery.isWindow( elem ) ?
|
|
elem :
|
|
elem.nodeType === 9 ?
|
|
elem.defaultView || elem.parentWindow :
|
|
false;
|
|
}
|