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 return { top: 0, left: 0 };
31 var doc = elem.ownerDocument,
33 docElem = doc.documentElement,
35 clientTop = docElem.clientTop || body.clientTop || 0,
36 clientLeft = docElem.clientLeft || body.clientLeft || 0,
37 scrollTop = (win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ),
38 scrollLeft = (win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft),
39 top = box.top + scrollTop - clientTop,
40 left = box.left + scrollLeft - clientLeft;
42 return { top: top, left: left };
46 jQuery.fn.offset = function( options ) {
50 return this.each(function( i ) {
51 jQuery.offset.setOffset( this, options, i );
55 if ( !elem || !elem.ownerDocument ) {
59 if ( elem === elem.ownerDocument.body ) {
60 return jQuery.offset.bodyOffset( elem );
63 jQuery.offset.initialize();
65 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
66 doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
67 body = doc.body, defaultView = doc.defaultView,
68 prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
69 top = elem.offsetTop, left = elem.offsetLeft;
71 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
72 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
76 computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
77 top -= elem.scrollTop;
78 left -= elem.scrollLeft;
80 if ( elem === offsetParent ) {
81 top += elem.offsetTop;
82 left += elem.offsetLeft;
84 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && rtable.test(elem.nodeName)) ) {
85 top += parseFloat( computedStyle.borderTopWidth ) || 0;
86 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
89 prevOffsetParent = offsetParent;
90 offsetParent = elem.offsetParent;
93 if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
94 top += parseFloat( computedStyle.borderTopWidth ) || 0;
95 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
98 prevComputedStyle = computedStyle;
101 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
102 top += body.offsetTop;
103 left += body.offsetLeft;
106 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
107 top += Math.max( docElem.scrollTop, body.scrollTop );
108 left += Math.max( docElem.scrollLeft, body.scrollLeft );
111 return { top: top, left: left };
116 initialize: function() {
117 var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop") ) || 0,
118 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>";
120 jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } );
122 container.innerHTML = html;
123 body.insertBefore( container, body.firstChild );
124 innerDiv = container.firstChild;
125 checkDiv = innerDiv.firstChild;
126 td = innerDiv.nextSibling.firstChild.firstChild;
128 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
129 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
131 checkDiv.style.position = "fixed";
132 checkDiv.style.top = "20px";
134 // safari subtracts parent border width here which is 5px
135 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
136 checkDiv.style.position = checkDiv.style.top = "";
138 innerDiv.style.overflow = "hidden";
139 innerDiv.style.position = "relative";
141 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
143 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
145 body.removeChild( container );
146 body = container = innerDiv = checkDiv = table = td = null;
147 jQuery.offset.initialize = jQuery.noop;
150 bodyOffset: function( body ) {
151 var top = body.offsetTop, left = body.offsetLeft;
153 jQuery.offset.initialize();
155 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
156 top += parseFloat( jQuery.css(body, "marginTop") ) || 0;
157 left += parseFloat( jQuery.css(body, "marginLeft") ) || 0;
160 return { top: top, left: left };
163 setOffset: function( elem, options, i ) {
164 var position = jQuery.css( elem, "position" );
166 // set position first, in-case top/left are set even on static elem
167 if ( position === "static" ) {
168 elem.style.position = "relative";
171 var curElem = jQuery( elem ),
172 curOffset = curElem.offset(),
173 curCSSTop = jQuery.css( elem, "top" ),
174 curCSSLeft = jQuery.css( elem, "left" ),
175 calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1),
176 props = {}, curPosition = {}, curTop, curLeft;
178 // need to be able to calculate position if either top or left is auto and position is absolute
179 if ( calculatePosition ) {
180 curPosition = curElem.position();
183 curTop = calculatePosition ? curPosition.top : parseInt( curCSSTop, 10 ) || 0;
184 curLeft = calculatePosition ? curPosition.left : parseInt( curCSSLeft, 10 ) || 0;
186 if ( jQuery.isFunction( options ) ) {
187 options = options.call( elem, i, curOffset );
190 if (options.top != null) {
191 props.top = (options.top - curOffset.top) + curTop;
193 if (options.left != null) {
194 props.left = (options.left - curOffset.left) + curLeft;
197 if ( "using" in options ) {
198 options.using.call( elem, props );
200 curElem.css( props );
207 position: function() {
214 // Get *real* offsetParent
215 offsetParent = this.offsetParent(),
217 // Get correct offsets
218 offset = this.offset(),
219 parentOffset = rroot.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
221 // Subtract element margins
222 // note: when an element has margin: auto the offsetLeft and marginLeft
223 // are the same in Safari causing offset.left to incorrectly be 0
224 offset.top -= parseFloat( jQuery.css(elem, "marginTop") ) || 0;
225 offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0;
227 // Add offsetParent borders
228 parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0;
229 parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0;
231 // Subtract the two offsets
233 top: offset.top - parentOffset.top,
234 left: offset.left - parentOffset.left
238 offsetParent: function() {
239 return this.map(function() {
240 var offsetParent = this.offsetParent || document.body;
241 while ( offsetParent && (!rroot.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
242 offsetParent = offsetParent.offsetParent;
250 // Create scrollLeft and scrollTop methods
251 jQuery.each( ["Left", "Top"], function( i, name ) {
252 var method = "scroll" + name;
254 jQuery.fn[ method ] = function(val) {
255 var elem = this[0], win;
261 if ( val !== undefined ) {
262 // Set the scroll offset
263 return this.each(function() {
264 win = getWindow( this );
268 !i ? val : jQuery(win).scrollLeft(),
269 i ? val : jQuery(win).scrollTop()
273 this[ method ] = val;
277 win = getWindow( elem );
279 // Return the scroll offset
280 return win ? ("pageXOffset" in win) ? win[ i ? "pageYOffset" : "pageXOffset" ] :
281 jQuery.support.boxModel && win.document.documentElement[ method ] ||
282 win.document.body[ method ] :
288 function getWindow( elem ) {
289 return jQuery.isWindow( elem ) ?
291 elem.nodeType === 9 ?
292 elem.defaultView || elem.parentWindow :