3 if ( "getBoundingClientRect" in document.documentElement ) {
4 jQuery.fn.offset = function( options ) {
8 return this.each(function( i ) {
9 jQuery.offset.setOffset( this, options, i );
13 if ( !elem || !elem.ownerDocument ) {
17 if ( elem === elem.ownerDocument.body ) {
18 return jQuery.offset.bodyOffset( elem );
21 var box = elem.getBoundingClientRect(),
22 doc = elem.ownerDocument,
24 docElem = doc.documentElement,
26 clientTop = docElem.clientTop || body.clientTop || 0,
27 clientLeft = docElem.clientLeft || body.clientLeft || 0,
28 scrollTop = (win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ),
29 scrollLeft = (win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft),
30 top = box.top + scrollTop - clientTop,
31 left = box.left + scrollLeft - clientLeft;
33 return { top: top, left: left };
37 jQuery.fn.offset = function( options ) {
41 return this.each(function( i ) {
42 jQuery.offset.setOffset( this, options, i );
46 if ( !elem || !elem.ownerDocument ) {
50 if ( elem === elem.ownerDocument.body ) {
51 return jQuery.offset.bodyOffset( elem );
54 jQuery.offset.initialize();
56 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
57 doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
58 body = doc.body, defaultView = doc.defaultView,
59 prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
60 top = elem.offsetTop, left = elem.offsetLeft;
62 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
63 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
67 computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
68 top -= elem.scrollTop;
69 left -= elem.scrollLeft;
71 if ( elem === offsetParent ) {
72 top += elem.offsetTop;
73 left += elem.offsetLeft;
75 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.nodeName)) ) {
76 top += parseFloat( computedStyle.borderTopWidth ) || 0;
77 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
80 prevOffsetParent = offsetParent;
81 offsetParent = elem.offsetParent;
84 if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
85 top += parseFloat( computedStyle.borderTopWidth ) || 0;
86 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
89 prevComputedStyle = computedStyle;
92 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
93 top += body.offsetTop;
94 left += body.offsetLeft;
97 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
98 top += Math.max( docElem.scrollTop, body.scrollTop );
99 left += Math.max( docElem.scrollLeft, body.scrollLeft );
102 return { top: top, left: left };
107 initialize: function() {
108 var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop") ) || 0,
109 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>";
111 jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } );
113 container.innerHTML = html;
114 body.insertBefore( container, body.firstChild );
115 innerDiv = container.firstChild;
116 checkDiv = innerDiv.firstChild;
117 td = innerDiv.nextSibling.firstChild.firstChild;
119 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
120 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
122 checkDiv.style.position = "fixed";
123 checkDiv.style.top = "20px";
125 // safari subtracts parent border width here which is 5px
126 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
127 checkDiv.style.position = checkDiv.style.top = "";
129 innerDiv.style.overflow = "hidden";
130 innerDiv.style.position = "relative";
132 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
134 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
136 body.removeChild( container );
137 body = container = innerDiv = checkDiv = table = td = null;
138 jQuery.offset.initialize = jQuery.noop;
141 bodyOffset: function( body ) {
142 var top = body.offsetTop, left = body.offsetLeft;
144 jQuery.offset.initialize();
146 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
147 top += parseFloat( jQuery.css(body, "marginTop") ) || 0;
148 left += parseFloat( jQuery.css(body, "marginLeft") ) || 0;
151 return { top: top, left: left };
154 setOffset: function( elem, options, i ) {
155 var position = jQuery.css( elem, "position" );
157 // set position first, in-case top/left are set even on static elem
158 if ( position === "static" ) {
159 elem.style.position = "relative";
162 var curElem = jQuery( elem ),
163 curOffset = curElem.offset(),
164 curCSSTop = jQuery.css( elem, "top" ),
165 curCSSLeft = jQuery.css( elem, "left" ),
166 calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1),
167 props = {}, curPosition = {}, curTop, curLeft;
169 // need to be able to calculate position if either top or left is auto and position is absolute
170 if ( calculatePosition ) {
171 curPosition = curElem.position();
174 curTop = calculatePosition ? curPosition.top : parseInt( curCSSTop, 10 ) || 0;
175 curLeft = calculatePosition ? curPosition.left : parseInt( curCSSLeft, 10 ) || 0;
177 if ( jQuery.isFunction( options ) ) {
178 options = options.call( elem, i, curOffset );
181 if (options.top != null) {
182 props.top = (options.top - curOffset.top) + curTop;
184 if (options.left != null) {
185 props.left = (options.left - curOffset.left) + curLeft;
188 if ( "using" in options ) {
189 options.using.call( elem, props );
191 curElem.css( props );
198 position: function() {
205 // Get *real* offsetParent
206 offsetParent = this.offsetParent(),
208 // Get correct offsets
209 offset = this.offset(),
210 parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
212 // Subtract element margins
213 // note: when an element has margin: auto the offsetLeft and marginLeft
214 // are the same in Safari causing offset.left to incorrectly be 0
215 offset.top -= parseFloat( jQuery.css(elem, "marginTop") ) || 0;
216 offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0;
218 // Add offsetParent borders
219 parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0;
220 parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0;
222 // Subtract the two offsets
224 top: offset.top - parentOffset.top,
225 left: offset.left - parentOffset.left
229 offsetParent: function() {
230 return this.map(function() {
231 var offsetParent = this.offsetParent || document.body;
232 while ( offsetParent && (!/^body|html$/i.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
233 offsetParent = offsetParent.offsetParent;
241 // Create scrollLeft and scrollTop methods
242 jQuery.each( ["Left", "Top"], function( i, name ) {
243 var method = "scroll" + name;
245 jQuery.fn[ method ] = function(val) {
246 var elem = this[0], win;
252 if ( val !== undefined ) {
253 // Set the scroll offset
254 return this.each(function() {
255 win = getWindow( this );
259 !i ? val : jQuery(win).scrollLeft(),
260 i ? val : jQuery(win).scrollTop()
264 this[ method ] = val;
268 win = getWindow( elem );
270 // Return the scroll offset
271 return win ? ("pageXOffset" in win) ? win[ i ? "pageYOffset" : "pageXOffset" ] :
272 jQuery.support.boxModel && win.document.documentElement[ method ] ||
273 win.document.body[ method ] :
279 function getWindow( elem ) {
280 return ("scrollTo" in elem && elem.document) ?
282 elem.nodeType === 9 ?
283 elem.defaultView || elem.parentWindow :