Made a number of spacing changes to bring the code more-inline with the jQuery Core...
[jquery.git] / src / offset.js
1 if ( "getBoundingClientRect" in document.documentElement ) {
2         jQuery.fn.offset = function( options ) {
3                 var elem = this[0];
4
5                 if ( !elem || !elem.ownerDocument ) {
6                         return null;
7                 }
8
9                 if ( options ) { 
10                         return this.each(function() {
11                                 jQuery.offset.setOffset( this, options );
12                         });
13                 }
14
15                 if ( elem === elem.ownerDocument.body ) {
16                         return jQuery.offset.bodyOffset( elem );
17                 }
18
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;
23
24                 return { top: top, left: left };
25         };
26
27 } else {
28         jQuery.fn.offset = function( options ) {
29                 var elem = this[0];
30
31                 if ( !elem || !elem.ownerDocument ) {
32                         return null;
33                 }
34
35                 if ( options ) { 
36                         return this.each(function() {
37                                 jQuery.offset.setOffset( this, options );
38                         });
39                 }
40
41                 if ( elem === elem.ownerDocument.body ) {
42                         return jQuery.offset.bodyOffset( elem );
43                 }
44
45                 jQuery.offset.initialize();
46
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;
52
53                 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
54                         if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
55                                 break;
56                         }
57
58                         computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
59                         top  -= elem.scrollTop;
60                         left -= elem.scrollLeft;
61
62                         if ( elem === offsetParent ) {
63                                 top  += elem.offsetTop;
64                                 left += elem.offsetLeft;
65
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;
69                                 }
70
71                                 prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
72                         }
73
74                         if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
75                                 top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
76                                 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
77                         }
78
79                         prevComputedStyle = computedStyle;
80                 }
81
82                 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
83                         top  += body.offsetTop;
84                         left += body.offsetLeft;
85                 }
86
87                 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
88                         top  += Math.max( docElem.scrollTop, body.scrollTop );
89                         left += Math.max( docElem.scrollLeft, body.scrollLeft );
90                 }
91
92                 return { top: top, left: left };
93         };
94 }
95
96 jQuery.offset = {
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>';
100
101                 jQuery.extend( container.style, { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' } );
102
103                 container.innerHTML = html;
104                 body.insertBefore( container, body.firstChild );
105                 innerDiv = container.firstChild;
106                 checkDiv = innerDiv.firstChild;
107                 td = innerDiv.nextSibling.firstChild.firstChild;
108
109                 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
110                 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
111
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 = '';
116
117                 innerDiv.style.overflow = 'hidden', innerDiv.style.position = 'relative';
118                 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
119
120                 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
121
122                 body.removeChild( container );
123                 body = container = innerDiv = checkDiv = table = td = null;
124                 jQuery.offset.initialize = function() {};
125         },
126
127         bodyOffset: function( body ) {
128                 var top = body.offsetTop, left = body.offsetLeft;
129
130                 jQuery.offset.initialize();
131
132                 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
133                         top  += parseFloat( jQuery.curCSS(body, 'marginTop',  true) ) || 0;
134                         left += parseFloat( jQuery.curCSS(body, 'marginLeft', true) ) || 0;
135                 }
136
137                 return { top: top, left: left };
138         },
139         
140         setOffset: function( elem, options ) {
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';
144                 }
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,
149                         props     = {
150                                 top:  (options.top  - curOffset.top)  + curTop,
151                                 left: (options.left - curOffset.left) + curLeft
152                         };
153                 
154                 if ( 'using' in options ) {
155                         options.using.call( elem, props );
156                 } else {
157                         curElem.css( props );
158                 }
159         }
160 };
161
162
163 jQuery.fn.extend({
164         position: function() {
165                 if ( !this[0] ) {
166                         return null;
167                 }
168
169                 var elem = this[0],
170
171                 // Get *real* offsetParent
172                 offsetParent = this.offsetParent(),
173
174                 // Get correct offsets
175                 offset       = this.offset(),
176                 parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
177
178                 // Subtract element margins
179                 // note: when an element has margin: auto the offsetLeft and marginLeft
180                 // are the same in Safari causing offset.left to incorrectly be 0
181                 offset.top  -= parseFloat( jQuery.curCSS(elem, 'marginTop',  true) ) || 0;
182                 offset.left -= parseFloat( jQuery.curCSS(elem, 'marginLeft', true) ) || 0;
183
184                 // Add offsetParent borders
185                 parentOffset.top  += parseFloat( jQuery.curCSS(offsetParent[0], 'borderTopWidth',  true) ) || 0;
186                 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true) ) || 0;
187
188                 // Subtract the two offsets
189                 return {
190                         top:  offset.top  - parentOffset.top,
191                         left: offset.left - parentOffset.left
192                 };
193         },
194
195         offsetParent: function() {
196                 return this.map(function() {
197                         var offsetParent = this.offsetParent || document.body;
198                         while ( offsetParent && (!/^body|html$/i.test(offsetParent.nodeName) && jQuery.css(offsetParent, 'position') === 'static') ) {
199                                 offsetParent = offsetParent.offsetParent;
200                         }
201                         return offsetParent;
202                 });
203         }
204 });
205
206
207 // Create scrollLeft and scrollTop methods
208 jQuery.each( ["Left", "Top"], function( i, name ) {
209         var method = "scroll" + name;
210
211         jQuery.fn[ method ] = function(val) {
212                 var elem = this[0], win;
213                 
214                 if ( !elem ) {
215                         return null;
216                 }
217
218                 if ( val !== undefined ) {
219                         // Set the scroll offset
220                         return this.each(function() {
221                                 win = getWindow( this );
222
223                                 if ( win ) {
224                                         win.scrollTo(
225                                                 !i ? val : jQuery(win).scrollLeft(),
226                                                  i ? val : jQuery(win).scrollTop()
227                                         );
228
229                                 } else {
230                                         this[ method ] = val;
231                                 }
232                         });
233                 } else {
234                         win = getWindow( elem );
235
236                         // Return the scroll offset
237                         return win ? ('pageXOffset' in win) ? win[ i ? 'pageYOffset' : 'pageXOffset' ] :
238                                 jQuery.support.boxModel && win.document.documentElement[ method ] ||
239                                         win.document.body[ method ] :
240                                 elem[ method ];
241                 }
242         };
243 });
244
245 function getWindow( elem ) {
246         return ("scrollTo" in elem && elem.document) ?
247                 elem :
248                 elem.nodeType === 9 ?
249                         elem.defaultView || elem.parentWindow :
250                         false;
251 }