offset: added fixed position support and fixed opera issue with borders on absolute...
[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                     offsetParent = elem.offsetParent, 
10                     doc          = elem.ownerDocument,
11                     safari2      = safari && parseInt(version) < 522,
12                     position     = jQuery.css(elem, "position"),
13                     absolute     = position == "absolute",
14                     fixed        = 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(
22                                 box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
23                                 box.top  + Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop)
24                         );
25                 
26                         // IE adds the HTML element's border, by default it is medium which is 2px
27                         // IE 6 and IE 7 quirks mode the border width is overwritable by the following css html { border: 0; }
28                         // IE 7 standards mode, the border is always 2px
29                         if ( msie ) {
30                                 var border = jQuery("html").css("borderWidth");
31                                 border = (border == "medium" || jQuery.boxModel && parseInt(version) >= 7) && 2 || border;
32                                 add( -border, -border );
33                         }
34         
35                 // Otherwise loop through the offsetParents and parentNodes
36                 } else {
37                 
38                         // Initial element offsets
39                         add( elem.offsetLeft, elem.offsetTop );
40                 
41                         // Get parent offsets
42                         while ( offsetParent ) {
43                                 // Add offsetParent offsets
44                                 add( offsetParent.offsetLeft, offsetParent.offsetTop );
45                         
46                                 // Mozilla and Safari > 2 does not include the border on offset parents
47                                 // However Mozilla adds the border for table cells
48                                 if ( mozilla && /^t[d|h]$/i.test(parent.tagName) || !safari2 )
49                                         border( offsetParent );
50                                         
51                                 // Get offsetParent's position
52                                 position = jQuery.css(offsetParent, "position");
53                                 
54                                 // Safari <= 2 doubles body offsets with an absolutely positioned element or parent
55                                 if ( safari2 && !absolute && position == "absolute" )
56                                         absolute = true;
57                                 
58                                 // Opera adds border for fixed, relative and absolute parent elements
59                                 if (opera && /^fixed|relative|absolute$/i.test(position))
60                                         add( 
61                                                 -parseInt(jQuery.css(elem, "borderLeftWidth")),
62                                                 -parseInt(jQuery.css(elem, "borderTopWidth"))
63                                         );
64                                         
65                                 // Add the document scroll offsets if position is fixed
66                                 if ( !fixed && position == "fixed" )
67                                         fixed = true;
68                         
69                                 // Get next offsetParent
70                                 offsetParent = offsetParent.offsetParent;
71                         }
72                 
73                         // Get parent scroll offsets
74                         while ( parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
75                                 // Work around opera inline/table scrollLeft/Top bug
76                                 if ( !/^inline|table-row.*$/i.test(jQuery.css(parent, "display")) )
77                                         // Subtract parent scroll offsets
78                                         add( -parent.scrollLeft, -parent.scrollTop );
79                         
80                                 // Mozilla does not add the border for a parent that has overflow != visible
81                                 if ( mozilla && jQuery.css(parent, "overflow") != "visible" )
82                                         border( parent );
83                         
84                                 // Get next parent
85                                 parent = parent.parentNode;
86                         }
87                 
88                         // Safari <= 2 doubles body offsets with an absolute or fixed positioned element or parent
89                         if ( safari2 && (absolute || fixed) )
90                                 add( -doc.body.offsetLeft, -doc.body.offsetTop );
91                         
92                         // Add the document scroll offsets if position is fixed
93                         if ( fixed )
94                                 add(
95                                         Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
96                                         Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop)
97                                 );
98                 }
99
100                 // Return an object with top and left properties
101                 results = { top: top, left: left };
102         }
103
104         return results;
105
106         function border(elem) {
107                 add( jQuery.css(elem, "borderLeftWidth"), jQuery.css(elem, "borderTopWidth") );
108         }
109
110         function add(l, t) {
111                 left += parseInt(l) || 0;
112                 top  += parseInt(t) || 0;
113         }
114 };