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 ) {
8 var elem = this[0], box;
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 );
25 box = elem.getBoundingClientRect();
28 var doc = elem.ownerDocument,
29 docElem = doc.documentElement;
31 // Make sure we're not dealing with a disconnected DOM node
32 if ( !box || !jQuery.contains( docElem, elem ) ) {
33 return box ? { top: box.top, left: box.left } : { top: 0, left: 0 };
38 clientTop = docElem.clientTop || body.clientTop || 0,
39 clientLeft = docElem.clientLeft || body.clientLeft || 0,
40 scrollTop = (win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ),
41 scrollLeft = (win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft),
42 top = box.top + scrollTop - clientTop,
43 left = box.left + scrollLeft - clientLeft;
45 return { top: top, left: left };
49 jQuery.fn.offset = function( options ) {
53 return this.each(function( i ) {
54 jQuery.offset.setOffset( this, options, i );
58 if ( !elem || !elem.ownerDocument ) {
62 if ( elem === elem.ownerDocument.body ) {
63 return jQuery.offset.bodyOffset( elem );
66 jQuery.offset.initialize();
69 offsetParent = elem.offsetParent,
70 prevOffsetParent = elem,
71 doc = elem.ownerDocument,
72 docElem = doc.documentElement,
74 defaultView = doc.defaultView,
75 prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
77 left = elem.offsetLeft;
79 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
80 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
84 computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
85 top -= elem.scrollTop;
86 left -= elem.scrollLeft;
88 if ( elem === offsetParent ) {
89 top += elem.offsetTop;
90 left += elem.offsetLeft;
92 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && rtable.test(elem.nodeName)) ) {
93 top += parseFloat( computedStyle.borderTopWidth ) || 0;
94 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
97 prevOffsetParent = offsetParent;
98 offsetParent = elem.offsetParent;
101 if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
102 top += parseFloat( computedStyle.borderTopWidth ) || 0;
103 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
106 prevComputedStyle = computedStyle;
109 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
110 top += body.offsetTop;
111 left += body.offsetLeft;
114 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
115 top += Math.max( docElem.scrollTop, body.scrollTop );
116 left += Math.max( docElem.scrollLeft, body.scrollLeft );
119 return { top: top, left: left };
124 initialize: function() {
125 var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop") ) || 0,
126 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>";
128 jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } );
130 container.innerHTML = html;
131 body.insertBefore( container, body.firstChild );
132 innerDiv = container.firstChild;
133 checkDiv = innerDiv.firstChild;
134 td = innerDiv.nextSibling.firstChild.firstChild;
136 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
137 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
139 checkDiv.style.position = "fixed";
140 checkDiv.style.top = "20px";
142 // safari subtracts parent border width here which is 5px
143 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
144 checkDiv.style.position = checkDiv.style.top = "";
146 innerDiv.style.overflow = "hidden";
147 innerDiv.style.position = "relative";
149 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
151 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
153 body.removeChild( container );
154 body = container = innerDiv = checkDiv = table = td = null;
155 jQuery.offset.initialize = jQuery.noop;
158 bodyOffset: function( body ) {
159 var top = body.offsetTop,
160 left = body.offsetLeft;
162 jQuery.offset.initialize();
164 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
165 top += parseFloat( jQuery.css(body, "marginTop") ) || 0;
166 left += parseFloat( jQuery.css(body, "marginLeft") ) || 0;
169 return { top: top, left: left };
172 setOffset: function( elem, options, i ) {
173 var position = jQuery.css( elem, "position" );
175 // set position first, in-case top/left are set even on static elem
176 if ( position === "static" ) {
177 elem.style.position = "relative";
180 var curElem = jQuery( elem ),
181 curOffset = curElem.offset(),
182 curCSSTop = jQuery.css( elem, "top" ),
183 curCSSLeft = jQuery.css( elem, "left" ),
184 calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1),
185 props = {}, curPosition = {}, curTop, curLeft;
187 // need to be able to calculate position if either top or left is auto and position is absolute
188 if ( calculatePosition ) {
189 curPosition = curElem.position();
192 curTop = calculatePosition ? curPosition.top : parseInt( curCSSTop, 10 ) || 0;
193 curLeft = calculatePosition ? curPosition.left : parseInt( curCSSLeft, 10 ) || 0;
195 if ( jQuery.isFunction( options ) ) {
196 options = options.call( elem, i, curOffset );
199 if (options.top != null) {
200 props.top = (options.top - curOffset.top) + curTop;
202 if (options.left != null) {
203 props.left = (options.left - curOffset.left) + curLeft;
206 if ( "using" in options ) {
207 options.using.call( elem, props );
209 curElem.css( props );
216 position: function() {
223 // Get *real* offsetParent
224 offsetParent = this.offsetParent(),
226 // Get correct offsets
227 offset = this.offset(),
228 parentOffset = rroot.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
230 // Subtract element margins
231 // note: when an element has margin: auto the offsetLeft and marginLeft
232 // are the same in Safari causing offset.left to incorrectly be 0
233 offset.top -= parseFloat( jQuery.css(elem, "marginTop") ) || 0;
234 offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0;
236 // Add offsetParent borders
237 parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0;
238 parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0;
240 // Subtract the two offsets
242 top: offset.top - parentOffset.top,
243 left: offset.left - parentOffset.left
247 offsetParent: function() {
248 return this.map(function() {
249 var offsetParent = this.offsetParent || document.body;
250 while ( offsetParent && (!rroot.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
251 offsetParent = offsetParent.offsetParent;
259 // Create scrollLeft and scrollTop methods
260 jQuery.each( ["Left", "Top"], function( i, name ) {
261 var method = "scroll" + name;
263 jQuery.fn[ method ] = function(val) {
264 var elem = this[0], win;
270 if ( val !== undefined ) {
271 // Set the scroll offset
272 return this.each(function() {
273 win = getWindow( this );
277 !i ? val : jQuery(win).scrollLeft(),
278 i ? val : jQuery(win).scrollTop()
282 this[ method ] = val;
286 win = getWindow( elem );
288 // Return the scroll offset
289 return win ? ("pageXOffset" in win) ? win[ i ? "pageYOffset" : "pageXOffset" ] :
290 jQuery.support.boxModel && win.document.documentElement[ method ] ||
291 win.document.body[ method ] :
297 function getWindow( elem ) {
298 return jQuery.isWindow( elem ) ?
300 elem.nodeType === 9 ?
301 elem.defaultView || elem.parentWindow :