Make sure that the submodules are pulling from the right location.
[jquery.git] / src / offset.js
1 if ( "getBoundingClientRect" in document.documentElement ) {
2         jQuery.fn.offset = function( options ) {
3                 var elem = this[0];
4                 if ( !elem || !elem.ownerDocument ) { return null; }
5                 if ( options ) { 
6                         return this.each(function() {
7                                 jQuery.offset.setOffset( this, options );
8                         });
9                 }
10                 if ( elem === elem.ownerDocument.body ) {
11                         return jQuery.offset.bodyOffset( elem );
12                 }
13
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 };
19         };
20 } else {
21         jQuery.fn.offset = function( options ) {
22                 var elem = this[0];
23                 if ( !elem || !elem.ownerDocument ) { return null; }
24                 if ( options ) { 
25                         return this.each(function() {
26                                 jQuery.offset.setOffset( this, options );
27                         });
28                 }
29                 if ( elem === elem.ownerDocument.body ) {
30                         return jQuery.offset.bodyOffset( elem );
31                 }
32
33                 jQuery.offset.initialize();
34
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;
40
41                 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
42                         if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) { break; }
43
44                         computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
45                         top  -= elem.scrollTop;
46                         left -= elem.scrollLeft;
47
48                         if ( elem === offsetParent ) {
49                                 top  += elem.offsetTop;
50                                 left += elem.offsetLeft;
51
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;
55                                 }
56
57                                 prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
58                         }
59
60                         if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
61                                 top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
62                                 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
63                         }
64
65                         prevComputedStyle = computedStyle;
66                 }
67
68                 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
69                         top  += body.offsetTop;
70                         left += body.offsetLeft;
71                 }
72
73                 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
74                         top  += Math.max( docElem.scrollTop, body.scrollTop );
75                         left += Math.max( docElem.scrollLeft, body.scrollLeft );
76                 }
77
78                 return { top: top, left: left };
79         };
80 }
81
82 jQuery.offset = {
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>';
86
87                 jQuery.extend( container.style, { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' } );
88
89                 container.innerHTML = html;
90                 body.insertBefore( container, body.firstChild );
91                 innerDiv = container.firstChild;
92                 checkDiv = innerDiv.firstChild;
93                 td = innerDiv.nextSibling.firstChild.firstChild;
94
95                 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
96                 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
97
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 = '';
102
103                 innerDiv.style.overflow = 'hidden', innerDiv.style.position = 'relative';
104                 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
105
106                 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
107
108                 body.removeChild( container );
109                 body = container = innerDiv = checkDiv = table = td = null;
110                 jQuery.offset.initialize = function(){};
111         },
112
113         bodyOffset: function( body ) {
114                 var top = body.offsetTop, left = body.offsetLeft;
115
116                 jQuery.offset.initialize();
117
118                 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
119                         top  += parseFloat( jQuery.curCSS(body, 'marginTop',  true) ) || 0;
120                         left += parseFloat( jQuery.curCSS(body, 'marginLeft', true) ) || 0;
121                 }
122
123                 return { top: top, left: left };
124         },
125         
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';
130                 }
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,
135                         props     = {
136                                 top:  (options.top  - curOffset.top)  + curTop,
137                                 left: (options.left - curOffset.left) + curLeft
138                         };
139                 
140                 if ( 'using' in options ) {
141                         options.using.call( elem, props );
142                 } else {
143                         curElem.css( props );
144                 }
145         }
146 };
147
148
149 jQuery.fn.extend({
150         position: function() {
151                 if ( !this[0] ) { return null; }
152
153                 var elem = this[0],
154
155                 // Get *real* offsetParent
156                 offsetParent = this.offsetParent(),
157
158                 // Get correct offsets
159                 offset       = this.offset(),
160                 parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
161
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;
167
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;
171
172                 // Subtract the two offsets
173                 return {
174                         top:  offset.top  - parentOffset.top,
175                         left: offset.left - parentOffset.left
176                 };
177         },
178
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;
184                         }
185                         return offsetParent;
186                 });
187         }
188 });
189
190
191 // Create scrollLeft and scrollTop methods
192 jQuery.each( ['Left', 'Top'], function(i, name) {
193         var method = 'scroll' + name;
194
195         jQuery.fn[ method ] = function(val) {
196                 var elem = this[0], win;
197                 
198                 if ( !elem ) { return null; }
199
200                 if ( val !== undefined ) {
201                         // Set the scroll offset
202                         return this.each(function() {
203                                 win = getWindow( this );
204
205                                 win ?
206                                         win.scrollTo(
207                                                 !i ? val : jQuery(win).scrollLeft(),
208                                                  i ? val : jQuery(win).scrollTop()
209                                         ) :
210                                         this[ method ] = val;
211                         });
212                 } else {
213                         win = getWindow( elem );
214
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 ] :
219                                 elem[ method ];
220                 }
221         };
222 });
223
224 function getWindow( elem ) {
225         return ("scrollTo" in elem && elem.document) ?
226                 elem :
227                 elem.nodeType === 9 ?
228                         elem.defaultView || elem.parentWindow :
229                         false;
230 }