1 if ( "getBoundingClientRect" in document.documentElement ) {
2 jQuery.fn.offset = function() {
4 if ( !elem || !elem.ownerDocument ) { return null; }
5 if ( elem === elem.ownerDocument.body ) {
6 return jQuery.offset.bodyOffset( elem );
9 var box = elem.getBoundingClientRect(), doc = elem.ownerDocument, body = doc.body, docElem = doc.documentElement,
10 clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
11 top = box.top + (self.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ) - clientTop,
12 left = box.left + (self.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft;
13 return { top: top, left: left };
16 jQuery.fn.offset = function() {
18 if ( !elem || !elem.ownerDocument ) { return null; }
19 if ( elem === elem.ownerDocument.body ) {
20 return jQuery.offset.bodyOffset( elem );
23 jQuery.offset.initialize();
25 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
26 doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
27 body = doc.body, defaultView = doc.defaultView,
28 prevComputedStyle = defaultView.getComputedStyle(elem, null),
29 top = elem.offsetTop, left = elem.offsetLeft;
31 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
32 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) { break; }
34 computedStyle = defaultView.getComputedStyle(elem, null);
35 top -= elem.scrollTop;
36 left -= elem.scrollLeft;
38 if ( elem === offsetParent ) {
39 top += elem.offsetTop;
40 left += elem.offsetLeft;
42 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.nodeName)) ) {
43 top += parseFloat( computedStyle.borderTopWidth ) || 0;
44 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
47 prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
50 if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
51 top += parseFloat( computedStyle.borderTopWidth ) || 0;
52 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
55 prevComputedStyle = computedStyle;
58 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
59 top += body.offsetTop;
60 left += body.offsetLeft;
63 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
64 top += Math.max( docElem.scrollTop, body.scrollTop );
65 left += Math.max( docElem.scrollLeft, body.scrollLeft );
68 return { top: top, left: left };
73 initialize: function() {
74 var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.curCSS(body, 'marginTop', true) ) || 0,
75 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>';
77 jQuery.extend( container.style, { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' } );
79 container.innerHTML = html;
80 body.insertBefore( container, body.firstChild );
81 innerDiv = container.firstChild;
82 checkDiv = innerDiv.firstChild;
83 td = innerDiv.nextSibling.firstChild.firstChild;
85 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
86 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
88 checkDiv.style.position = 'fixed', checkDiv.style.top = '20px';
89 // safari subtracts parent border width here which is 5px
90 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
91 checkDiv.style.position = checkDiv.style.top = '';
93 innerDiv.style.overflow = 'hidden', innerDiv.style.position = 'relative';
94 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
96 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
98 body.removeChild( container );
99 body = container = innerDiv = checkDiv = table = td = null;
100 jQuery.offset.initialize = function(){};
103 bodyOffset: function(body) {
104 var top = body.offsetTop, left = body.offsetLeft;
106 jQuery.offset.initialize();
108 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
109 top += parseFloat( jQuery.curCSS(body, 'marginTop', true) ) || 0;
110 left += parseFloat( jQuery.curCSS(body, 'marginLeft', true) ) || 0;
113 return { top: top, left: left };
119 position: function() {
120 if ( !this[0] ) { return null; }
124 // Get *real* offsetParent
125 offsetParent = this.offsetParent(),
127 // Get correct offsets
128 offset = this.offset(),
129 parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
131 // Subtract element margins
132 // note: when an element has margin: auto the offsetLeft and marginLeft
133 // are the same in Safari causing offset.left to incorrectly be 0
134 offset.top -= parseFloat( jQuery.curCSS(elem, 'marginTop', true) ) || 0;
135 offset.left -= parseFloat( jQuery.curCSS(elem, 'marginLeft', true) ) || 0;
137 // Add offsetParent borders
138 parentOffset.top += parseFloat( jQuery.curCSS(offsetParent[0], 'borderTopWidth', true) ) || 0;
139 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true) ) || 0;
141 // Subtract the two offsets
143 top: offset.top - parentOffset.top,
144 left: offset.left - parentOffset.left
148 offsetParent: function() {
149 return this.map(function(){
150 var offsetParent = this.offsetParent || document.body;
151 while ( offsetParent && (!/^body|html$/i.test(offsetParent.nodeName) && jQuery.css(offsetParent, 'position') === 'static') ) {
152 offsetParent = offsetParent.offsetParent;
160 // Create scrollLeft and scrollTop methods
161 jQuery.each( ['Left', 'Top'], function(i, name) {
162 var method = 'scroll' + name;
164 jQuery.fn[ method ] = function(val) {
165 var elem = this[0], win;
167 if ( !elem ) { return null; }
169 if ( val !== undefined ) {
170 // Set the scroll offset
171 return this.each(function() {
172 win = getWindow( this );
176 !i ? val : jQuery(win).scrollLeft(),
177 i ? val : jQuery(win).scrollTop()
179 this[ method ] = val;
182 win = getWindow( elem );
184 // Return the scroll offset
185 return win ? ('pageXOffset' in win) ? win[ i ? 'pageYOffset' : 'pageXOffset' ] :
186 jQuery.support.boxModel && win.document.documentElement[ method ] ||
187 win.document.body[ method ] :
193 function getWindow( elem ) {
194 return ("scrollTo" in elem && elem.document) ?
196 elem.nodeType === 9 ?
197 elem.defaultView || elem.parentWindow :