Merged dimensions with core
[jquery.git] / src / offset.js
1 // The Offset Method
2 // Originally By Brandon Aaron, part of the Dimension Plugin
3 // http://jquery.com/plugins/project/dimensions
4 jQuery.fn.offset = function() {
5         var left = 0, top = 0, elem = this[0], results;
6         
7         if ( elem ) with ( jQuery.browser ) {
8                 var parent       = elem.parentNode, 
9                     offsetChild  = elem,
10                     offsetParent = elem.offsetParent, 
11                     doc          = elem.ownerDocument,
12                     safari2      = safari && parseInt(version) < 522 && !/adobeair/i.test(userAgent),
13                     css          = jQuery.curCSS,
14                     fixed        = css(elem, "position") == "fixed";
15         
16                 // Use getBoundingClientRect if available
17                 if ( elem.getBoundingClientRect ) {
18                         var box = elem.getBoundingClientRect();
19                 
20                         // Add the document scroll offsets
21                         add(box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
22                                 box.top  + Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop));
23                 
24                         // IE adds the HTML element's border, by default it is medium which is 2px
25                         // IE 6 and 7 quirks mode the border width is overwritable by the following css html { border: 0; }
26                         // IE 7 standards mode, the border is always 2px
27                         // This border/offset is typically represented by the clientLeft and clientTop properties
28                         // However, in IE6 and 7 quirks mode the clientLeft and clientTop properties are not updated when overwriting it via CSS
29                         // Therefore this method will be off by 2px in IE while in quirksmode
30                         add( -doc.documentElement.clientLeft, -doc.documentElement.clientTop );
31         
32                 // Otherwise loop through the offsetParents and parentNodes
33                 } else {
34                 
35                         // Initial element offsets
36                         add( elem.offsetLeft, elem.offsetTop );
37                         
38                         // Get parent offsets
39                         while ( offsetParent ) {
40                                 // Add offsetParent offsets
41                                 add( offsetParent.offsetLeft, offsetParent.offsetTop );
42                         
43                                 // Mozilla and Safari > 2 does not include the border on offset parents
44                                 // However Mozilla adds the border for table or table cells
45                                 if ( mozilla && !/^t(able|d|h)$/i.test(offsetParent.tagName) || safari && !safari2 )
46                                         border( offsetParent );
47                                         
48                                 // Add the document scroll offsets if position is fixed on any offsetParent
49                                 if ( !fixed && css(offsetParent, "position") == "fixed" )
50                                         fixed = true;
51                         
52                                 // Set offsetChild to previous offsetParent unless it is the body element
53                                 offsetChild  = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent;
54                                 // Get next offsetParent
55                                 offsetParent = offsetParent.offsetParent;
56                         }
57                 
58                         // Get parent scroll offsets
59                         while ( parent && parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
60                                 // Remove parent scroll UNLESS that parent is inline or a table to work around Opera inline/table scrollLeft/Top bug
61                                 if ( !/^inline|table.*$/i.test(css(parent, "display")) )
62                                         // Subtract parent scroll offsets
63                                         add( -parent.scrollLeft, -parent.scrollTop );
64                         
65                                 // Mozilla does not add the border for a parent that has overflow != visible
66                                 if ( mozilla && css(parent, "overflow") != "visible" )
67                                         border( parent );
68                         
69                                 // Get next parent
70                                 parent = parent.parentNode;
71                         }
72                 
73                         // Safari <= 2 doubles body offsets with a fixed position element/offsetParent or absolutely positioned offsetChild
74                         // Mozilla doubles body offsets with a non-absolutely positioned offsetChild
75                         if ( (safari2 && (fixed || css(offsetChild, "position") == "absolute")) || 
76                                 (mozilla && css(offsetChild, "position") != "absolute") )
77                                         add( -doc.body.offsetLeft, -doc.body.offsetTop );
78                         
79                         // Add the document scroll offsets if position is fixed
80                         if ( fixed )
81                                 add(Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
82                                         Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop));
83                 }
84
85                 // Return an object with top and left properties
86                 results = { top: top, left: left };
87         }
88
89         function border(elem) {
90                 add( jQuery.curCSS(elem, "borderLeftWidth", true), jQuery.curCSS(elem, "borderTopWidth", true) );
91         }
92
93         function add(l, t) {
94                 left += parseInt(l) || 0;
95                 top += parseInt(t) || 0;
96         }
97
98         return results;
99 };
100
101
102 jQuery.fn.extend({
103         position: function() {
104                 var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
105                 
106                 if (elem) {
107                         // Get *real* offsetParent
108                         offsetParent = this.offsetParent();
109                         
110                         // Get correct offsets
111                         offset       = this.offset();
112                         parentOffset = offsetParent.offset();
113                         
114                         // Subtract element margins
115                         offset.top  -= parseInt( jQuery.curCSS(elem, 'marginTop', true) ) || 0;
116                         offset.left -= parseInt( jQuery.curCSS(elem, 'marginLeft', true) ) || 0;
117                         
118                         // Add offsetParent borders
119                         parentOffset.top  += parseInt( jQuery.curCSS(offsetParent[0], 'borderTopWidth', true) ) || 0;
120                         parentOffset.left += parseInt( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true) ) || 0;
121                         
122                         // Subtract the two offsets
123                         results = {
124                                 top:  offset.top  - parentOffset.top,
125                                 left: offset.left - parentOffset.left
126                         };
127                 }
128                 
129                 return results;
130         },
131         
132         offsetParent: function() {
133                 var offsetParent = this[0].offsetParent;
134                 while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static') )
135                         offsetParent = offsetParent.offsetParent;
136                 return jQuery(offsetParent);
137         }
138 });
139
140
141 // Create scrollLeft and scrollTop methods
142 jQuery.each( ['Left', 'Top'], function(i, name) {
143         jQuery.fn[ 'scroll' + name ] = function(val) {
144                 if (!this[0]) return;
145                 
146                 return val != undefined ?
147                 
148                         // Set the scroll offset
149                         this.each(function() {
150                                 this == window || this == document ?
151                                         window.scrollTo( 
152                                                 name == 'Left' ? val : jQuery(window)[ 'scrollLeft' ](),
153                                                 name == 'Top'  ? val : jQuery(window)[ 'scrollTop'  ]()
154                                         ) :
155                                         this[ 'scroll' + name ] = val;
156                         }) :
157                         
158                         // Return the scroll offset
159                         this[0] == window || this[0] == document ?
160                                 self[ (name == 'Left' ? 'pageXOffset' : 'pageYOffset') ] ||
161                                         jQuery.boxModel && document.documentElement[ 'scroll' + name ] ||
162                                         document.body[ 'scroll' + name ] :
163                                 this[0][ 'scroll' + name ];
164         };
165 });