1 if ( "getBoundingClientRect" in document.documentElement ) {
2 jQuery.fn.offset = function( options ) {
4 if ( !elem || !elem.ownerDocument ) { return null; }
6 return this.each(function() {
7 jQuery.offset.setOffset( this, options );
10 if ( elem === elem.ownerDocument.body ) {
11 return jQuery.offset.bodyOffset( elem );
14 var box = elem.getBoundingClientRect(), doc = elem.ownerDocument, body = doc.body, docElem = doc.documentElement,
15 clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
16 top = box.top + (self.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ) - clientTop,
17 left = box.left + (self.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft;
18 return { top: top, left: left };
21 jQuery.fn.offset = function( options ) {
23 if ( !elem || !elem.ownerDocument ) { return null; }
25 return this.each(function() {
26 jQuery.offset.setOffset( this, options );
29 if ( elem === elem.ownerDocument.body ) {
30 return jQuery.offset.bodyOffset( elem );
33 jQuery.offset.initialize();
35 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
36 doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
37 body = doc.body, defaultView = doc.defaultView,
38 prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
39 top = elem.offsetTop, left = elem.offsetLeft;
41 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
42 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) { break; }
44 computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
45 top -= elem.scrollTop;
46 left -= elem.scrollLeft;
48 if ( elem === offsetParent ) {
49 top += elem.offsetTop;
50 left += elem.offsetLeft;
52 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.nodeName)) ) {
53 top += parseFloat( computedStyle.borderTopWidth ) || 0;
54 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
57 prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
60 if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
61 top += parseFloat( computedStyle.borderTopWidth ) || 0;
62 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
65 prevComputedStyle = computedStyle;
68 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
69 top += body.offsetTop;
70 left += body.offsetLeft;
73 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
74 top += Math.max( docElem.scrollTop, body.scrollTop );
75 left += Math.max( docElem.scrollLeft, body.scrollLeft );
78 return { top: top, left: left };
83 initialize: function() {
84 var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.curCSS(body, 'marginTop', true) ) || 0,
85 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>';
87 jQuery.extend( container.style, { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' } );
89 container.innerHTML = html;
90 body.insertBefore( container, body.firstChild );
91 innerDiv = container.firstChild;
92 checkDiv = innerDiv.firstChild;
93 td = innerDiv.nextSibling.firstChild.firstChild;
95 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
96 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
98 checkDiv.style.position = 'fixed', checkDiv.style.top = '20px';
99 // safari subtracts parent border width here which is 5px
100 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
101 checkDiv.style.position = checkDiv.style.top = '';
103 innerDiv.style.overflow = 'hidden', innerDiv.style.position = 'relative';
104 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
106 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
108 body.removeChild( container );
109 body = container = innerDiv = checkDiv = table = td = null;
110 jQuery.offset.initialize = function(){};
113 bodyOffset: function( body ) {
114 var top = body.offsetTop, left = body.offsetLeft;
116 jQuery.offset.initialize();
118 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
119 top += parseFloat( jQuery.curCSS(body, 'marginTop', true) ) || 0;
120 left += parseFloat( jQuery.curCSS(body, 'marginLeft', true) ) || 0;
123 return { top: top, left: left };
126 setOffset: function( elem, options ) {
127 // set position first, in-case top/left are set even on static elem
128 if ( /static/.test( jQuery.curCSS( elem, 'position' ) ) ) {
129 elem.style.position = 'relative';
131 var curElem = jQuery( elem ),
132 curOffset = curElem.offset(),
133 curTop = parseInt( jQuery.curCSS( elem, 'top', true ), 10 ) || 0,
134 curLeft = parseInt( jQuery.curCSS( elem, 'left', true ), 10) || 0,
136 top: (options.top - curOffset.top) + curTop,
137 left: (options.left - curOffset.left) + curLeft
140 if ( 'using' in options ) {
141 options.using.call( elem, props );
143 curElem.css( props );
150 position: function() {
151 if ( !this[0] ) { return null; }
155 // Get *real* offsetParent
156 offsetParent = this.offsetParent(),
158 // Get correct offsets
159 offset = this.offset(),
160 parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
162 // Subtract element margins
163 // note: when an element has margin: auto the offsetLeft and marginLeft
164 // are the same in Safari causing offset.left to incorrectly be 0
165 offset.top -= parseFloat( jQuery.curCSS(elem, 'marginTop', true) ) || 0;
166 offset.left -= parseFloat( jQuery.curCSS(elem, 'marginLeft', true) ) || 0;
168 // Add offsetParent borders
169 parentOffset.top += parseFloat( jQuery.curCSS(offsetParent[0], 'borderTopWidth', true) ) || 0;
170 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true) ) || 0;
172 // Subtract the two offsets
174 top: offset.top - parentOffset.top,
175 left: offset.left - parentOffset.left
179 offsetParent: function() {
180 return this.map(function(){
181 var offsetParent = this.offsetParent || document.body;
182 while ( offsetParent && (!/^body|html$/i.test(offsetParent.nodeName) && jQuery.css(offsetParent, 'position') === 'static') ) {
183 offsetParent = offsetParent.offsetParent;
191 // Create scrollLeft and scrollTop methods
192 jQuery.each( ['Left', 'Top'], function(i, name) {
193 var method = 'scroll' + name;
195 jQuery.fn[ method ] = function(val) {
196 var elem = this[0], win;
198 if ( !elem ) { return null; }
200 if ( val !== undefined ) {
201 // Set the scroll offset
202 return this.each(function() {
203 win = getWindow( this );
207 !i ? val : jQuery(win).scrollLeft(),
208 i ? val : jQuery(win).scrollTop()
210 this[ method ] = val;
213 win = getWindow( elem );
215 // Return the scroll offset
216 return win ? ('pageXOffset' in win) ? win[ i ? 'pageYOffset' : 'pageXOffset' ] :
217 jQuery.support.boxModel && win.document.documentElement[ method ] ||
218 win.document.body[ method ] :
224 function getWindow( elem ) {
225 return ("scrollTo" in elem && elem.document) ?
227 elem.nodeType === 9 ?
228 elem.defaultView || elem.parentWindow :