offset now uses clientLeft and clientTop instead of calculating html border in IE
[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,
13                     fixed        = jQuery.css(elem, "position") == "fixed";
14         
15                 // Use getBoundingClientRect if available
16                 if ( elem.getBoundingClientRect ) {
17                         var box = elem.getBoundingClientRect();
18                 
19                         // Add the document scroll offsets
20                         add(
21                                 box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
22                                 box.top  + Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop)
23                         );
24                 
25                         // IE adds the HTML element's border, by default it is medium which is 2px
26                         // IE 6 and 7 quirks mode the border width is overwritable by the following css html { border: 0; }
27                         // IE 7 standards mode, the border is always 2px
28                         // This border/offset is typically represented by the clientLeft and clientTop properties
29                         // However, in IE6 and 7 quirks mode the clientLeft and clientTop properties are not updated when overwriting it via CSS
30                         // Therefore this method will be off by 2px in IE while in quirksmode
31                         add( -doc.documentElement.clientLeft, -doc.documentElement.clientTop );
32         
33                 // Otherwise loop through the offsetParents and parentNodes
34                 } else {
35                 
36                         // Initial element offsets
37                         add( elem.offsetLeft, elem.offsetTop );
38                 
39                         // Get parent offsets
40                         while ( offsetParent ) {
41                                 // Add offsetParent offsets
42                                 add( offsetParent.offsetLeft, offsetParent.offsetTop );
43                         
44                                 // Mozilla and Safari > 2 does not include the border on offset parents
45                                 // However Mozilla adds the border for table or table cells
46                                 if ( mozilla && !/^t(able|d|h)$/i.test(offsetParent.tagName) || safari && !safari2 )
47                                         border( offsetParent );
48                                         
49                                 // Add the document scroll offsets if position is fixed on any offsetParent
50                                 if ( !fixed && jQuery.css(offsetParent, "position") == "fixed" )
51                                         fixed = true;
52                         
53                                 // Set offsetChild to previous offsetParent unless it is the body element
54                                 offsetChild  = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent;
55                                 // Get next offsetParent
56                                 offsetParent = offsetParent.offsetParent;
57                         }
58                 
59                         // Get parent scroll offsets
60                         while ( parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
61                                 // Remove parent scroll UNLESS that parent is inline or a table-row to work around Opera inline/table scrollLeft/Top bug
62                                 if ( !/^inline|table-row.*$/i.test(jQuery.css(parent, "display")) )
63                                         // Subtract parent scroll offsets
64                                         add( -parent.scrollLeft, -parent.scrollTop );
65                         
66                                 // Mozilla does not add the border for a parent that has overflow != visible
67                                 if ( mozilla && jQuery.css(parent, "overflow") != "visible" )
68                                         border( parent );
69                         
70                                 // Get next parent
71                                 parent = parent.parentNode;
72                         }
73                 
74                         // Safari <= 2 doubles body offsets with a fixed position element/offsetParent or absolutely positioned offsetChild
75                         // Mozilla doubles body offsets with a non-absolutely positioned offsetChild
76                         if ( (safari2 && (fixed || jQuery.css(offsetChild, "position") == "absolute")) || 
77                                 (mozilla && jQuery.css(offsetChild, "position") != "absoltue") )
78                                         add( -doc.body.offsetLeft, -doc.body.offsetTop );
79                         
80                         // Add the document scroll offsets if position is fixed
81                         if ( fixed )
82                                 add(
83                                         Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
84                                         Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop)
85                                 );
86                 }
87
88                 // Return an object with top and left properties
89                 results = { top: top, left: left };
90         }
91
92         return results;
93
94         function border(elem) {
95                 add( jQuery.css(elem, "borderLeftWidth"), jQuery.css(elem, "borderTopWidth") );
96         }
97
98         function add(l, t) {
99                 left += parseInt(l) || 0;
100                 top += parseInt(t) || 0;
101         }
102 };