offset: fixes for correct body offsets in safari and mozilla (thanks Wizzud)
[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 IE 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                         if ( msie ) {
29                                 var border = jQuery("html").css("borderWidth");
30                                 border = (border == "medium" || jQuery.boxModel && parseInt(version) >= 7) && 2 || border;
31                                 add( -border, -border );
32                         }
33         
34                 // Otherwise loop through the offsetParents and parentNodes
35                 } else {
36                 
37                         // Initial element offsets
38                         add( elem.offsetLeft, elem.offsetTop );
39                 
40                         // Get parent offsets
41                         while ( offsetParent ) {
42                                 // Add offsetParent offsets
43                                 add( offsetParent.offsetLeft, offsetParent.offsetTop );
44                         
45                                 // Mozilla and Safari > 2 does not include the border on offset parents
46                                 // However Mozilla adds the border for table or table cells
47                                 if ( mozilla && !/^t(able|d|h)$/i.test(offsetParent.tagName) || safari && !safari2 )
48                                         border( offsetParent );
49                                         
50                                 // Add the document scroll offsets if position is fixed on any offsetParent
51                                 if ( !fixed && jQuery.css(offsetParent, "position") == "fixed" )
52                                         fixed = true;
53                         
54                                 // Set offsetChild to previous offsetParent unless it is the body element
55                                 offsetChild  = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent;
56                                 // Get next offsetParent
57                                 offsetParent = offsetParent.offsetParent;
58                         }
59                 
60                         // Get parent scroll offsets
61                         while ( parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
62                                 // Remove parent scroll UNLESS that parent is inline or a table-row to work around Opera inline/table scrollLeft/Top bug
63                                 if ( !/^inline|table-row.*$/i.test(jQuery.css(parent, "display")) )
64                                         // Subtract parent scroll offsets
65                                         add( -parent.scrollLeft, -parent.scrollTop );
66                         
67                                 // Mozilla does not add the border for a parent that has overflow != visible
68                                 if ( mozilla && jQuery.css(parent, "overflow") != "visible" )
69                                         border( parent );
70                         
71                                 // Get next parent
72                                 parent = parent.parentNode;
73                         }
74                 
75                         // Safari <= 2 doubles body offsets with a fixed position element/offsetParent or absolutely positioned offsetChild
76                         // Mozilla doubles body offsets with a non-absolutely positioned offsetChild
77                         if ( (safari2 && (fixed || jQuery.css(offsetChild, "position") == "absolute")) || 
78                                 (mozilla && jQuery.css(offsetChild, "position") != "absoltue") )
79                                         add( -doc.body.offsetLeft, -doc.body.offsetTop );
80                         
81                         // Add the document scroll offsets if position is fixed
82                         if ( fixed )
83                                 add(
84                                         Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
85                                         Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop)
86                                 );
87                 }
88
89                 // Return an object with top and left properties
90                 results = { top: top, left: left };
91         }
92
93         return results;
94
95         function border(elem) {
96                 add( jQuery.css(elem, "borderLeftWidth"), jQuery.css(elem, "borderTopWidth") );
97         }
98
99         function add(l, t) {
100                 left += parseInt(l) || 0;
101                 top += parseInt(t) || 0;
102         }
103 };