3 var rtable = /^t(?:able|d|h)$/i,
4 rroot = /^(?:body|html)$/i;
6 if ( "getBoundingClientRect" in document.documentElement ) {
7 jQuery.fn.offset = function( options ) {
11 return this.each(function( i ) {
12 jQuery.offset.setOffset( this, options, i );
16 if ( !elem || !elem.ownerDocument ) {
20 if ( elem === elem.ownerDocument.body ) {
21 return jQuery.offset.bodyOffset( elem );
24 var box = elem.getBoundingClientRect(),
25 doc = elem.ownerDocument,
27 docElem = doc.documentElement,
29 clientTop = docElem.clientTop || body.clientTop || 0,
30 clientLeft = docElem.clientLeft || body.clientLeft || 0,
31 scrollTop = (win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ),
32 scrollLeft = (win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft),
33 top = box.top + scrollTop - clientTop,
34 left = box.left + scrollLeft - clientLeft;
36 return { top: top, left: left };
40 jQuery.fn.offset = function( options ) {
44 return this.each(function( i ) {
45 jQuery.offset.setOffset( this, options, i );
49 if ( !elem || !elem.ownerDocument ) {
53 if ( elem === elem.ownerDocument.body ) {
54 return jQuery.offset.bodyOffset( elem );
57 jQuery.offset.initialize();
59 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
60 doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
61 body = doc.body, defaultView = doc.defaultView,
62 prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
63 top = elem.offsetTop, left = elem.offsetLeft;
65 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
66 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
70 computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
71 top -= elem.scrollTop;
72 left -= elem.scrollLeft;
74 if ( elem === offsetParent ) {
75 top += elem.offsetTop;
76 left += elem.offsetLeft;
78 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && rtable.test(elem.nodeName)) ) {
79 top += parseFloat( computedStyle.borderTopWidth ) || 0;
80 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
83 prevOffsetParent = offsetParent;
84 offsetParent = elem.offsetParent;
87 if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
88 top += parseFloat( computedStyle.borderTopWidth ) || 0;
89 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
92 prevComputedStyle = computedStyle;
95 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
96 top += body.offsetTop;
97 left += body.offsetLeft;
100 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
101 top += Math.max( docElem.scrollTop, body.scrollTop );
102 left += Math.max( docElem.scrollLeft, body.scrollLeft );
105 return { top: top, left: left };
110 initialize: function() {
111 var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop") ) || 0,
112 html = "<div style='position:absolute;top:0;left:0;margin:0;border:5px solid #000;padding:0;width:1px;height:1px;'><div></div></div><table style='position:absolute;top:0;left:0;margin:0;border:5px solid #000;padding:0;width:1px;height:1px;' cellpadding='0' cellspacing='0'><tr><td></td></tr></table>";
114 jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } );
116 container.innerHTML = html;
117 body.insertBefore( container, body.firstChild );
118 innerDiv = container.firstChild;
119 checkDiv = innerDiv.firstChild;
120 td = innerDiv.nextSibling.firstChild.firstChild;
122 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
123 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
125 checkDiv.style.position = "fixed";
126 checkDiv.style.top = "20px";
128 // safari subtracts parent border width here which is 5px
129 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
130 checkDiv.style.position = checkDiv.style.top = "";
132 innerDiv.style.overflow = "hidden";
133 innerDiv.style.position = "relative";
135 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
137 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
139 body.removeChild( container );
140 body = container = innerDiv = checkDiv = table = td = null;
141 jQuery.offset.initialize = jQuery.noop;
144 bodyOffset: function( body ) {
145 var top = body.offsetTop, left = body.offsetLeft;
147 jQuery.offset.initialize();
149 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
150 top += parseFloat( jQuery.css(body, "marginTop") ) || 0;
151 left += parseFloat( jQuery.css(body, "marginLeft") ) || 0;
154 return { top: top, left: left };
157 setOffset: function( elem, options, i ) {
158 var position = jQuery.css( elem, "position" );
160 // set position first, in-case top/left are set even on static elem
161 if ( position === "static" ) {
162 elem.style.position = "relative";
165 var curElem = jQuery( elem ),
166 curOffset = curElem.offset(),
167 curCSSTop = jQuery.css( elem, "top" ),
168 curCSSLeft = jQuery.css( elem, "left" ),
169 calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1),
170 props = {}, curPosition = {}, curTop, curLeft;
172 // need to be able to calculate position if either top or left is auto and position is absolute
173 if ( calculatePosition ) {
174 curPosition = curElem.position();
177 curTop = calculatePosition ? curPosition.top : parseInt( curCSSTop, 10 ) || 0;
178 curLeft = calculatePosition ? curPosition.left : parseInt( curCSSLeft, 10 ) || 0;
180 if ( jQuery.isFunction( options ) ) {
181 options = options.call( elem, i, curOffset );
184 if (options.top != null) {
185 props.top = (options.top - curOffset.top) + curTop;
187 if (options.left != null) {
188 props.left = (options.left - curOffset.left) + curLeft;
191 if ( "using" in options ) {
192 options.using.call( elem, props );
194 curElem.css( props );
201 position: function() {
208 // Get *real* offsetParent
209 offsetParent = this.offsetParent(),
211 // Get correct offsets
212 offset = this.offset(),
213 parentOffset = rroot.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
215 // Subtract element margins
216 // note: when an element has margin: auto the offsetLeft and marginLeft
217 // are the same in Safari causing offset.left to incorrectly be 0
218 offset.top -= parseFloat( jQuery.css(elem, "marginTop") ) || 0;
219 offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0;
221 // Add offsetParent borders
222 parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0;
223 parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0;
225 // Subtract the two offsets
227 top: offset.top - parentOffset.top,
228 left: offset.left - parentOffset.left
232 offsetParent: function() {
233 return this.map(function() {
234 var offsetParent = this.offsetParent || document.body;
235 while ( offsetParent && (!rroot.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
236 offsetParent = offsetParent.offsetParent;
244 // Create scrollLeft and scrollTop methods
245 jQuery.each( ["Left", "Top"], function( i, name ) {
246 var method = "scroll" + name;
248 jQuery.fn[ method ] = function(val) {
249 var elem = this[0], win;
255 if ( val !== undefined ) {
256 // Set the scroll offset
257 return this.each(function() {
258 win = getWindow( this );
262 !i ? val : jQuery(win).scrollLeft(),
263 i ? val : jQuery(win).scrollTop()
267 this[ method ] = val;
271 win = getWindow( elem );
273 // Return the scroll offset
274 return win ? ("pageXOffset" in win) ? win[ i ? "pageYOffset" : "pageXOffset" ] :
275 jQuery.support.boxModel && win.document.documentElement[ method ] ||
276 win.document.body[ method ] :
282 function getWindow( elem ) {
283 return jQuery.isWindow( elem ) ?
285 elem.nodeType === 9 ?
286 elem.defaultView || elem.parentWindow :