Removed some cases of strict errors.
[jquery.git] / src / offset.js
1 if ( "getBoundingClientRect" in document.documentElement )
2         jQuery.fn.offset = function() {
3                 var elem = this[0];
4                 if ( !elem || !elem.ownerDocument ) 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 || !elem.ownerDocument ) 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                 body = container = innerDiv = checkDiv = table = td = null;
80         },
81
82         bodyOffset: function(body) {
83                 jQuery.offset.initialize();
84                 var top = body.offsetTop, left = body.offsetLeft;
85                 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset )
86                         top  += parseFloat( jQuery.curCSS(body, 'marginTop',  true) ) || 0,
87                         left += parseFloat( jQuery.curCSS(body, 'marginLeft', true) ) || 0;
88                 return { top: top, left: left };
89         }
90 };
91
92
93 jQuery.fn.extend({
94         position: function() {
95                 if ( !this[0] ) return null;
96
97                 var elem = this[0],
98
99                 // Get *real* offsetParent
100                 offsetParent = this.offsetParent(),
101
102                 // Get correct offsets
103                 offset       = this.offset(),
104                 parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
105
106                 // Subtract element margins
107                 // note: when an element has margin: auto the offsetLeft and marginLeft
108                 // are the same in Safari causing offset.left to incorrectly be 0
109                 offset.top  -= parseFloat( jQuery.curCSS(elem, 'marginTop',  true) ) || 0;
110                 offset.left -= parseFloat( jQuery.curCSS(elem, 'marginLeft', true) ) || 0;
111
112                 // Add offsetParent borders
113                 parentOffset.top  += parseFloat( jQuery.curCSS(offsetParent[0], 'borderTopWidth',  true) ) || 0;
114                 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true) ) || 0;
115
116                 // Subtract the two offsets
117                 return {
118                         top:  offset.top  - parentOffset.top,
119                         left: offset.left - parentOffset.left
120                 };
121         },
122
123         offsetParent: function() {
124                 var offsetParent = this[0].offsetParent || document.body;
125                 while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') === 'static') )
126                         offsetParent = offsetParent.offsetParent;
127                 return jQuery( offsetParent );
128         }
129 });
130
131
132 // Create scrollLeft and scrollTop methods
133 jQuery.each( ['Left', 'Top'], function(i, name) {
134         var method = 'scroll' + name;
135
136         jQuery.fn[ method ] = function(val) {
137                 if ( !this[0] ) return null;
138                 
139                 var elem = this[0], win = ("scrollTo" in elem && elem.document) ? elem :
140                         (elem.nodeName === "#document") ? elem.defaultView || elem.parentWindow :
141                                 false;
142
143                 return val !== undefined ?
144
145                         // Set the scroll offset
146                         this.each(function() {
147                                 win = ("scrollTo" in this && this.document) ? this : 
148                                         (this.nodeName === "#document") ? this.defaultView || this.parentWindow :
149                                                 false;
150                                 
151                                 win ?
152                                         win.scrollTo(
153                                                 !i ? val : jQuery(win).scrollLeft(),
154                                                  i ? val : jQuery(win).scrollTop()
155                                         ) :
156                                         this[ method ] = val;
157                         }) :
158
159                         // Return the scroll offset
160                         win ?
161                                 win[ i ? 'pageYOffset' : 'pageXOffset' ] ||
162                                         jQuery.support.boxModel && win.document.documentElement[ method ] ||
163                                         win.document.body[ method ] :
164                                 elem[ method ];
165         };
166 });