Reintroduced .offset() as a default include, added original author credits.
[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     absolute        = jQuery.css(elem, "position") == "absolute", 
9                         parent          = elem.parentNode, 
10                         offsetParent    = elem.offsetParent, 
11                         doc             = elem.ownerDocument,
12                         safari2         = safari && !absolute && parseInt(version) < 522;
13         
14                 // Use getBoundingClientRect if available
15                 if ( elem.getBoundingClientRect ) {
16                         box = elem.getBoundingClientRect();
17                 
18                         // Add the document scroll offsets
19                         add(
20                                 box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
21                                 box.top  + Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop)
22                         );
23                 
24                         // IE adds the HTML element's border, by default it is medium which is 2px
25                         // IE 6 and IE 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                         if ( msie ) {
28                                 var border = jQuery("html").css("borderWidth");
29                                 border = (border == "medium" || jQuery.boxModel && parseInt(version) >= 7) && 2 || border;
30                                 add( -border, -border );
31                         }
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 cells
46                                 if ( mozilla && /^t[d|h]$/i.test(parent.tagName) || !safari2 )
47                                         border( offsetParent );
48                                 
49                                 // Safari <= 2 doubles body offsets with an absolutely positioned element or parent
50                                 if ( safari2 && !absolute && jQuery.css(offsetParent, "position") == "absolute" )
51                                         absolute = true;
52                         
53                                 // Get next offsetParent
54                                 offsetParent = offsetParent.offsetParent;
55                         }
56                 
57                         // Get parent scroll offsets
58                         while ( parent.tagName && /^body|html$/i.test(parent.tagName) ) {
59                                 // Work around opera inline/table scrollLeft/Top bug
60                                 if ( /^inline|table-row.*$/i.test(jQuery.css(parent, "display")) )
61                                         // Subtract parent scroll offsets
62                                         add( -parent.scrollLeft, -parent.scrollTop );
63                         
64                                 // Mozilla does not add the border for a parent that has overflow != visible
65                                 if ( mozilla && jQuery.css(parent, "overflow") != "visible" )
66                                         border( parent );
67                         
68                                 // Get next parent
69                                 parent = parent.parentNode;
70                         }
71                 
72                         // Safari doubles body offsets with an absolutely positioned element or parent
73                         if ( safari && absolute )
74                                 add( -doc.body.offsetLeft, -doc.body.offsetTop );
75                 }
76
77                 // Return an object with top and left properties
78                 results = { top: top, left: left };
79         }
80
81         return results;
82
83         function border(elem) {
84                 add( jQuery.css(elem, "borderLeftWidth"), jQuery.css(elem, "borderTopWidth") );
85         }
86
87         function add(l, t) {
88                 left += parseInt(l) || 0;
89                 top += parseInt(t) || 0;
90         }
91 };