mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
181 lines
4.8 KiB
JavaScript
181 lines
4.8 KiB
JavaScript
var rroot = /^(?:body|html)$/i;
|
|
|
|
jQuery.fn.offset = function( options ) {
|
|
if ( arguments.length ) {
|
|
return options === undefined ?
|
|
this :
|
|
this.each(function( i ) {
|
|
jQuery.offset.setOffset( this, options, i );
|
|
});
|
|
}
|
|
|
|
var box, docElem, body, win, clientTop, clientLeft, scrollTop, scrollLeft, top, left,
|
|
elem = this[ 0 ],
|
|
doc = elem && elem.ownerDocument;
|
|
|
|
if ( !doc ) {
|
|
return null;
|
|
}
|
|
|
|
if ( (body = doc.body) === elem ) {
|
|
return jQuery.offset.bodyOffset( elem );
|
|
}
|
|
|
|
docElem = doc.documentElement;
|
|
|
|
// Make sure we're not dealing with a disconnected DOM node
|
|
if ( !jQuery.contains( docElem, elem ) ) {
|
|
return { top: 0, left: 0 };
|
|
}
|
|
|
|
box = elem.getBoundingClientRect();
|
|
win = getWindow( doc );
|
|
clientTop = docElem.clientTop || body.clientTop || 0;
|
|
clientLeft = docElem.clientLeft || body.clientLeft || 0;
|
|
scrollTop = win.pageYOffset || docElem.scrollTop;
|
|
scrollLeft = win.pageXOffset || docElem.scrollLeft;
|
|
top = box.top + scrollTop - clientTop;
|
|
left = box.left + scrollLeft - clientLeft;
|
|
|
|
return { top: top, left: left };
|
|
};
|
|
|
|
jQuery.offset = {
|
|
|
|
bodyOffset: function( body ) {
|
|
var top = body.offsetTop,
|
|
left = body.offsetLeft;
|
|
|
|
if ( jQuery.support.doesNotIncludeMarginInBodyOffset ) {
|
|
top += parseFloat( jQuery.css(body, "marginTop") ) || 0;
|
|
left += parseFloat( jQuery.css(body, "marginLeft") ) || 0;
|
|
}
|
|
|
|
return { top: top, left: left };
|
|
},
|
|
|
|
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 null;
|
|
}
|
|
|
|
var elem = this[0],
|
|
|
|
// Get *real* offsetParent
|
|
offsetParent = this.offsetParent(),
|
|
|
|
// Get correct offsets
|
|
offset = this.offset(),
|
|
parentOffset = rroot.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
|
|
|
|
// Subtract 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
|
|
offset.top -= parseFloat( jQuery.css(elem, "marginTop") ) || 0;
|
|
offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0;
|
|
|
|
// Add offsetParent borders
|
|
parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0;
|
|
parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0;
|
|
|
|
// Subtract the two offsets
|
|
return {
|
|
top: offset.top - parentOffset.top,
|
|
left: offset.left - parentOffset.left
|
|
};
|
|
},
|
|
|
|
offsetParent: function() {
|
|
return this.map(function() {
|
|
var offsetParent = this.offsetParent || document.body;
|
|
while ( offsetParent && (!rroot.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
|
|
offsetParent = offsetParent.offsetParent;
|
|
}
|
|
return offsetParent;
|
|
});
|
|
}
|
|
});
|
|
|
|
|
|
// 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;
|
|
}
|