1 if ( "getBoundingClientRect" in document.documentElement ) {
2 jQuery.fn.offset = function( options ) {
5 if ( !elem || !elem.ownerDocument ) {
10 return this.each(function( i ) {
11 jQuery.offset.setOffset( this, options, i );
15 if ( elem === elem.ownerDocument.body ) {
16 return jQuery.offset.bodyOffset( elem );
19 var box = elem.getBoundingClientRect(), doc = elem.ownerDocument, body = doc.body, docElem = doc.documentElement,
20 clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
21 top = box.top + (self.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ) - clientTop,
22 left = box.left + (self.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft;
24 return { top: top, left: left };
28 jQuery.fn.offset = function( options ) {
31 if ( !elem || !elem.ownerDocument ) {
36 return this.each(function( i ) {
37 jQuery.offset.setOffset( this, options, i );
41 if ( elem === elem.ownerDocument.body ) {
42 return jQuery.offset.bodyOffset( elem );
45 jQuery.offset.initialize();
47 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
48 doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
49 body = doc.body, defaultView = doc.defaultView,
50 prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
51 top = elem.offsetTop, left = elem.offsetLeft;
53 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
54 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
58 computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
59 top -= elem.scrollTop;
60 left -= elem.scrollLeft;
62 if ( elem === offsetParent ) {
63 top += elem.offsetTop;
64 left += elem.offsetLeft;
66 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.nodeName)) ) {
67 top += parseFloat( computedStyle.borderTopWidth ) || 0;
68 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
71 prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
74 if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
75 top += parseFloat( computedStyle.borderTopWidth ) || 0;
76 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
79 prevComputedStyle = computedStyle;
82 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
83 top += body.offsetTop;
84 left += body.offsetLeft;
87 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
88 top += Math.max( docElem.scrollTop, body.scrollTop );
89 left += Math.max( docElem.scrollLeft, body.scrollLeft );
92 return { top: top, left: left };
97 initialize: function() {
98 var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.curCSS(body, "marginTop", true) ) || 0,
99 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>";
101 jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } );
103 container.innerHTML = html;
104 body.insertBefore( container, body.firstChild );
105 innerDiv = container.firstChild;
106 checkDiv = innerDiv.firstChild;
107 td = innerDiv.nextSibling.firstChild.firstChild;
109 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
110 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
112 checkDiv.style.position = "fixed", checkDiv.style.top = "20px";
113 // safari subtracts parent border width here which is 5px
114 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
115 checkDiv.style.position = checkDiv.style.top = "";
117 innerDiv.style.overflow = "hidden", innerDiv.style.position = "relative";
118 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
120 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
122 body.removeChild( container );
123 body = container = innerDiv = checkDiv = table = td = null;
124 jQuery.offset.initialize = jQuery.noop;
127 bodyOffset: function( body ) {
128 var top = body.offsetTop, left = body.offsetLeft;
130 jQuery.offset.initialize();
132 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
133 top += parseFloat( jQuery.curCSS(body, "marginTop", true) ) || 0;
134 left += parseFloat( jQuery.curCSS(body, "marginLeft", true) ) || 0;
137 return { top: top, left: left };
140 setOffset: function( elem, options, i ) {
141 // set position first, in-case top/left are set even on static elem
142 if ( /static/.test( jQuery.curCSS( elem, "position" ) ) ) {
143 elem.style.position = "relative";
145 var curElem = jQuery( elem ),
146 curOffset = curElem.offset(),
147 curTop = parseInt( jQuery.curCSS( elem, "top", true ), 10 ) || 0,
148 curLeft = parseInt( jQuery.curCSS( elem, "left", true ), 10 ) || 0;
150 if ( jQuery.isFunction( options ) ) {
151 options = options.call( elem, i, curOffset );
155 top: (options.top - curOffset.top) + curTop,
156 left: (options.left - curOffset.left) + curLeft
159 if ( "using" in options ) {
160 options.using.call( elem, props );
162 curElem.css( props );
169 position: function() {
176 // Get *real* offsetParent
177 offsetParent = this.offsetParent(),
179 // Get correct offsets
180 offset = this.offset(),
181 parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
183 // Subtract element margins
184 // note: when an element has margin: auto the offsetLeft and marginLeft
185 // are the same in Safari causing offset.left to incorrectly be 0
186 offset.top -= parseFloat( jQuery.curCSS(elem, "marginTop", true) ) || 0;
187 offset.left -= parseFloat( jQuery.curCSS(elem, "marginLeft", true) ) || 0;
189 // Add offsetParent borders
190 parentOffset.top += parseFloat( jQuery.curCSS(offsetParent[0], "borderTopWidth", true) ) || 0;
191 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], "borderLeftWidth", true) ) || 0;
193 // Subtract the two offsets
195 top: offset.top - parentOffset.top,
196 left: offset.left - parentOffset.left
200 offsetParent: function() {
201 return this.map(function() {
202 var offsetParent = this.offsetParent || document.body;
203 while ( offsetParent && (!/^body|html$/i.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
204 offsetParent = offsetParent.offsetParent;
212 // Create scrollLeft and scrollTop methods
213 jQuery.each( ["Left", "Top"], function( i, name ) {
214 var method = "scroll" + name;
216 jQuery.fn[ method ] = function(val) {
217 var elem = this[0], win;
223 if ( val !== undefined ) {
224 // Set the scroll offset
225 return this.each(function() {
226 win = getWindow( this );
230 !i ? val : jQuery(win).scrollLeft(),
231 i ? val : jQuery(win).scrollTop()
235 this[ method ] = val;
239 win = getWindow( elem );
241 // Return the scroll offset
242 return win ? ("pageXOffset" in win) ? win[ i ? "pageYOffset" : "pageXOffset" ] :
243 jQuery.support.boxModel && win.document.documentElement[ method ] ||
244 win.document.body[ method ] :
250 function getWindow( elem ) {
251 return ("scrollTo" in elem && elem.document) ?
253 elem.nodeType === 9 ?
254 elem.defaultView || elem.parentWindow :