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