remove radix param for parseFloat calls
[jquery.git] / src / offset.js
1 if ( "getBoundingClientRect" in document.documentElement )
2         jQuery.fn.offset = function() {
3                 var elem = this[0];
4                 if ( !elem ) return null;
5                 if ( elem === elem.ownerDocument.body ) return jQuery.offset.bodyOffset( elem );
6                 var box = elem.getBoundingClientRect(), doc = elem.ownerDocument, body = doc.body, docElem = doc.documentElement,
7                         clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
8                         top  = box.top  + (self.pageYOffset || jQuery.support.boxModel && docElem.scrollTop  || body.scrollTop ) - clientTop,
9                         left = box.left + (self.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft;
10                 return { top: top, left: left };
11         };
12 else
13         jQuery.fn.offset = function() {
14                 var elem = this[0];
15                 if ( !elem ) return null;
16                 if ( elem === elem.ownerDocument.body ) return jQuery.offset.bodyOffset( elem );
17                 jQuery.offset.initialize();
18
19                 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
20                         doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
21                         body = doc.body, defaultView = doc.defaultView,
22                         prevComputedStyle = defaultView.getComputedStyle(elem, null),
23                         top = elem.offsetTop, left = elem.offsetLeft;
24
25                 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
26                         if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) break;
27                         computedStyle = defaultView.getComputedStyle(elem, null);
28                         top -= elem.scrollTop, left -= elem.scrollLeft;
29                         if ( elem === offsetParent ) {
30                                 top += elem.offsetTop, left += elem.offsetLeft;
31                                 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.tagName)) )
32                                         top  += parseFloat( computedStyle.borderTopWidth  ) || 0,
33                                         left += parseFloat( computedStyle.borderLeftWidth ) || 0;
34                                 prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
35                         }
36                         if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" )
37                                 top  += parseFloat( computedStyle.borderTopWidth  ) || 0,
38                                 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
39                         prevComputedStyle = computedStyle;
40                 }
41
42                 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" )
43                         top  += body.offsetTop,
44                         left += body.offsetLeft;
45
46                 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" )
47                         top  += Math.max( docElem.scrollTop, body.scrollTop ),
48                         left += Math.max( docElem.scrollLeft, body.scrollLeft );
49
50                 return { top: top, left: left };
51         };
52
53 jQuery.offset = {
54         initialize: function() {
55                 var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.curCSS(body, 'marginTop', true) ) || 0,
56                         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>';
57
58                 jQuery.extend( container.style, { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' } );
59
60                 container.innerHTML = html;
61                 body.insertBefore( container, body.firstChild );
62                 innerDiv = container.firstChild, checkDiv = innerDiv.firstChild, td = innerDiv.nextSibling.firstChild.firstChild;
63
64                 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
65                 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
66
67                 checkDiv.style.position = 'fixed', checkDiv.style.top = '20px';
68                 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15); // safari subtracts parent border width here which is 5px
69                 checkDiv.style.position = '', checkDiv.style.top = '';
70
71                 innerDiv.style.overflow = 'hidden', innerDiv.style.position = 'relative';
72                 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
73
74                 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
75
76                 body.removeChild( container );
77                 jQuery.offset.initialize = function(){};
78         },
79
80         bodyOffset: function(body) {
81                 jQuery.offset.initialize();
82                 var top = body.offsetTop, left = body.offsetLeft;
83                 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset )
84                         top  += parseFloat( jQuery.curCSS(body, 'marginTop',  true) ) || 0,
85                         left += parseFloat( jQuery.curCSS(body, 'marginLeft', true) ) || 0;
86                 return { top: top, left: left };
87         }
88 };
89
90
91 jQuery.fn.extend({
92         position: function() {
93                 if ( !this[0] ) return null;
94
95                 var elem = this[0],
96
97                 // Get *real* offsetParent
98                 offsetParent = this.offsetParent(),
99
100                 // Get correct offsets
101                 offset       = this.offset(),
102                 parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
103
104                 // Subtract element margins
105                 // note: when an element has margin: auto the offsetLeft and marginLeft
106                 // are the same in Safari causing offset.left to incorrectly be 0
107                 offset.top  -= parseFloat( jQuery.curCSS(elem, 'marginTop',  true) ) || 0;
108                 offset.left -= parseFloat( jQuery.curCSS(elem, 'marginLeft', true) ) || 0;
109
110                 // Add offsetParent borders
111                 parentOffset.top  += parseFloat( jQuery.curCSS(offsetParent[0], 'borderTopWidth',  true) ) || 0;
112                 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true) ) || 0;
113
114                 // Subtract the two offsets
115                 return {
116                         top:  offset.top  - parentOffset.top,
117                         left: offset.left - parentOffset.left
118                 };
119         },
120
121         offsetParent: function() {
122                 var offsetParent = this[0].offsetParent || document.body;
123                 while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') === 'static') )
124                         offsetParent = offsetParent.offsetParent;
125                 return jQuery( offsetParent );
126         }
127 });
128
129
130 // Create scrollLeft and scrollTop methods
131 jQuery.each( ['Left', 'Top'], function(i, name) {
132         var method = 'scroll' + name;
133
134         jQuery.fn[ method ] = function(val) {
135                 if ( !this[0] ) return null;
136                 
137                 var elem = this[0], win = ("scrollTo" in elem && elem.document) ? elem :
138                         (elem.nodeName === "#document") ? elem.defaultView || elem.parentWindow :
139                                 false;
140
141                 return val !== undefined ?
142
143                         // Set the scroll offset
144                         this.each(function() {
145                                 win = ("scrollTo" in this && this.document) ? this : 
146                                         (this.nodeName === "#document") ? this.defaultView || this.parentWindow :
147                                                 false;
148                                 
149                                 win ?
150                                         win.scrollTo(
151                                                 !i ? val : jQuery(win).scrollLeft(),
152                                                  i ? val : jQuery(win).scrollTop()
153                                         ) :
154                                         this[ method ] = val;
155                         }) :
156
157                         // Return the scroll offset
158                         win ?
159                                 win[ i ? 'pageYOffset' : 'pageXOffset' ] ||
160                                         jQuery.support.boxModel && win.document.documentElement[ method ] ||
161                                         win.document.body[ method ] :
162                                 elem[ method ];
163         };
164 });