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, docElem = doc.documentElement,
6 top = box.top + (self.pageYOffset || jQuery.boxModel && docElem.scrollTop || doc.body.scrollTop ) - docElem.clientTop,
7 left = box.left + (self.pageXOffset || jQuery.boxModel && docElem.scrollLeft || doc.body.scrollLeft) - docElem.clientLeft;
8 return { top: top, left: left };
11 jQuery.fn.offset = function() {
12 if ( !this[0] ) return { top: 0, left: 0 };
13 if ( this[0] === this[0].ownerDocument.body ) return jQuery.offset.bodyOffset( this[0] );
14 jQuery.offset.initialized || jQuery.offset.initialize();
16 var elem = this[0], offsetParent = elem.offsetParent, prevOffsetParent = elem,
17 doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
18 body = doc.body, defaultView = doc.defaultView,
19 prevComputedStyle = defaultView.getComputedStyle(elem, null),
20 top = elem.offsetTop, left = elem.offsetLeft;
22 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
23 computedStyle = defaultView.getComputedStyle(elem, null);
24 top -= elem.scrollTop, left -= elem.scrollLeft;
25 if ( elem === offsetParent ) {
26 top += elem.offsetTop, left += elem.offsetLeft;
27 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.tagName)) )
28 top += parseInt( computedStyle.borderTopWidth, 10) || 0,
29 left += parseInt( computedStyle.borderLeftWidth, 10) || 0;
30 prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
32 if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" )
33 top += parseInt( computedStyle.borderTopWidth, 10) || 0,
34 left += parseInt( computedStyle.borderLeftWidth, 10) || 0;
35 prevComputedStyle = computedStyle;
38 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" )
39 top += body.offsetTop,
40 left += body.offsetLeft;
42 if ( prevComputedStyle.position === "fixed" )
43 top += Math.max(docElem.scrollTop, body.scrollTop),
44 left += Math.max(docElem.scrollLeft, body.scrollLeft);
46 return { top: top, left: left };
50 initialize: function() {
51 if ( this.initialized ) return;
52 var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, rules, prop, bodyMarginTop = body.style.marginTop,
53 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 rules = { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' }
56 for ( prop in rules ) container.style[prop] = rules[prop];
58 container.innerHTML = html;
59 body.insertBefore(container, body.firstChild);
60 innerDiv = container.firstChild, checkDiv = innerDiv.firstChild, td = innerDiv.nextSibling.firstChild.firstChild;
62 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
63 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
65 innerDiv.style.overflow = 'hidden', innerDiv.style.position = 'relative';
66 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
68 body.style.marginTop = '1px';
69 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop === 0);
70 body.style.marginTop = bodyMarginTop;
72 body.removeChild(container);
73 this.initialized = true;
76 bodyOffset: function(body) {
77 jQuery.offset.initialized || jQuery.offset.initialize();
78 var top = body.offsetTop, left = body.offsetLeft;
79 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset )
80 top += parseInt( jQuery.curCSS(body, 'marginTop', true), 10 ) || 0,
81 left += parseInt( jQuery.curCSS(body, 'marginLeft', true), 10 ) || 0;
82 return { top: top, left: left };
88 position: function() {
89 var left = 0, top = 0, results;
92 // Get *real* offsetParent
93 var offsetParent = this.offsetParent(),
95 // Get correct offsets
96 offset = this.offset(),
97 parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
99 // Subtract element margins
100 // note: when an element has margin: auto the offsetLeft and marginLeft
101 // are the same in Safari causing offset.left to incorrectly be 0
102 offset.top -= num( this, 'marginTop' );
103 offset.left -= num( this, 'marginLeft' );
105 // Add offsetParent borders
106 parentOffset.top += num( offsetParent, 'borderTopWidth' );
107 parentOffset.left += num( offsetParent, 'borderLeftWidth' );
109 // Subtract the two offsets
111 top: offset.top - parentOffset.top,
112 left: offset.left - parentOffset.left
119 offsetParent: function() {
120 var offsetParent = this[0].offsetParent || document.body;
121 while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static') )
122 offsetParent = offsetParent.offsetParent;
123 return jQuery(offsetParent);
128 // Create scrollLeft and scrollTop methods
129 jQuery.each( ['Left', 'Top'], function(i, name) {
130 var method = 'scroll' + name;
132 jQuery.fn[ method ] = function(val) {
133 if (!this[0]) return;
135 return val != undefined ?
137 // Set the scroll offset
138 this.each(function() {
139 this == window || this == document ?
141 !i ? val : jQuery(window).scrollLeft(),
142 i ? val : jQuery(window).scrollTop()
144 this[ method ] = val;
147 // Return the scroll offset
148 this[0] == window || this[0] == document ?
149 self[ i ? 'pageYOffset' : 'pageXOffset' ] ||
150 jQuery.boxModel && document.documentElement[ method ] ||
151 document.body[ method ] :