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: 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();
68 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
69 doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
70 body = doc.body, defaultView = doc.defaultView,
71 prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
72 top = elem.offsetTop, left = elem.offsetLeft;
74 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
75 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
79 computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
80 top -= elem.scrollTop;
81 left -= elem.scrollLeft;
83 if ( elem === offsetParent ) {
84 top += elem.offsetTop;
85 left += elem.offsetLeft;
87 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && rtable.test(elem.nodeName)) ) {
88 top += parseFloat( computedStyle.borderTopWidth ) || 0;
89 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
92 prevOffsetParent = offsetParent;
93 offsetParent = elem.offsetParent;
96 if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
97 top += parseFloat( computedStyle.borderTopWidth ) || 0;
98 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
101 prevComputedStyle = computedStyle;
104 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
105 top += body.offsetTop;
106 left += body.offsetLeft;
109 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
110 top += Math.max( docElem.scrollTop, body.scrollTop );
111 left += Math.max( docElem.scrollLeft, body.scrollLeft );
114 return { top: top, left: left };
119 initialize: function() {
120 var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop") ) || 0,
121 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>";
123 jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } );
125 container.innerHTML = html;
126 body.insertBefore( container, body.firstChild );
127 innerDiv = container.firstChild;
128 checkDiv = innerDiv.firstChild;
129 td = innerDiv.nextSibling.firstChild.firstChild;
131 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
132 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
134 checkDiv.style.position = "fixed";
135 checkDiv.style.top = "20px";
137 // safari subtracts parent border width here which is 5px
138 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
139 checkDiv.style.position = checkDiv.style.top = "";
141 innerDiv.style.overflow = "hidden";
142 innerDiv.style.position = "relative";
144 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
146 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
148 body.removeChild( container );
149 body = container = innerDiv = checkDiv = table = td = null;
150 jQuery.offset.initialize = jQuery.noop;
153 bodyOffset: function( body ) {
154 var top = body.offsetTop, left = body.offsetLeft;
156 jQuery.offset.initialize();
158 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
159 top += parseFloat( jQuery.css(body, "marginTop") ) || 0;
160 left += parseFloat( jQuery.css(body, "marginLeft") ) || 0;
163 return { top: top, left: left };
166 setOffset: function( elem, options, i ) {
167 var position = jQuery.css( elem, "position" );
169 // set position first, in-case top/left are set even on static elem
170 if ( position === "static" ) {
171 elem.style.position = "relative";
174 var curElem = jQuery( elem ),
175 curOffset = curElem.offset(),
176 curCSSTop = jQuery.css( elem, "top" ),
177 curCSSLeft = jQuery.css( elem, "left" ),
178 calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1),
179 props = {}, curPosition = {}, curTop, curLeft;
181 // need to be able to calculate position if either top or left is auto and position is absolute
182 if ( calculatePosition ) {
183 curPosition = curElem.position();
186 curTop = calculatePosition ? curPosition.top : parseInt( curCSSTop, 10 ) || 0;
187 curLeft = calculatePosition ? curPosition.left : parseInt( curCSSLeft, 10 ) || 0;
189 if ( jQuery.isFunction( options ) ) {
190 options = options.call( elem, i, curOffset );
193 if (options.top != null) {
194 props.top = (options.top - curOffset.top) + curTop;
196 if (options.left != null) {
197 props.left = (options.left - curOffset.left) + curLeft;
200 if ( "using" in options ) {
201 options.using.call( elem, props );
203 curElem.css( props );
210 position: function() {
217 // Get *real* offsetParent
218 offsetParent = this.offsetParent(),
220 // Get correct offsets
221 offset = this.offset(),
222 parentOffset = rroot.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
224 // Subtract element margins
225 // note: when an element has margin: auto the offsetLeft and marginLeft
226 // are the same in Safari causing offset.left to incorrectly be 0
227 offset.top -= parseFloat( jQuery.css(elem, "marginTop") ) || 0;
228 offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0;
230 // Add offsetParent borders
231 parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0;
232 parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0;
234 // Subtract the two offsets
236 top: offset.top - parentOffset.top,
237 left: offset.left - parentOffset.left
241 offsetParent: function() {
242 return this.map(function() {
243 var offsetParent = this.offsetParent || document.body;
244 while ( offsetParent && (!rroot.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
245 offsetParent = offsetParent.offsetParent;
253 // Create scrollLeft and scrollTop methods
254 jQuery.each( ["Left", "Top"], function( i, name ) {
255 var method = "scroll" + name;
257 jQuery.fn[ method ] = function(val) {
258 var elem = this[0], win;
264 if ( val !== undefined ) {
265 // Set the scroll offset
266 return this.each(function() {
267 win = getWindow( this );
271 !i ? val : jQuery(win).scrollLeft(),
272 i ? val : jQuery(win).scrollTop()
276 this[ method ] = val;
280 win = getWindow( elem );
282 // Return the scroll offset
283 return win ? ("pageXOffset" in win) ? win[ i ? "pageYOffset" : "pageXOffset" ] :
284 jQuery.support.boxModel && win.document.documentElement[ method ] ||
285 win.document.body[ method ] :
291 function getWindow( elem ) {
292 return jQuery.isWindow( elem ) ?
294 elem.nodeType === 9 ?
295 elem.defaultView || elem.parentWindow :