Imported the innerHeight and outerHeight methods from the Dimensions plugin.
[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                     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(box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
21                                 box.top  + Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop));
22                 
23                         // IE adds the HTML element's border, by default it is medium which is 2px
24                         // IE 6 and 7 quirks mode the border width is overwritable by the following css html { border: 0; }
25                         // IE 7 standards mode, the border is always 2px
26                         // This border/offset is typically represented by the clientLeft and clientTop properties
27                         // However, in IE6 and 7 quirks mode the clientLeft and clientTop properties are not updated when overwriting it via CSS
28                         // Therefore this method will be off by 2px in IE while in quirksmode
29                         add( -doc.documentElement.clientLeft, -doc.documentElement.clientTop );
30         
31                 // Otherwise loop through the offsetParents and parentNodes
32                 } else {
33                 
34                         // Initial element offsets
35                         add( elem.offsetLeft, elem.offsetTop );
36                         
37                         // Get parent offsets
38                         while ( offsetParent ) {
39                                 // Add offsetParent offsets
40                                 add( offsetParent.offsetLeft, offsetParent.offsetTop );
41                         
42                                 // Mozilla and Safari > 2 does not include the border on offset parents
43                                 // However Mozilla adds the border for table or table cells
44                                 if ( mozilla && !/^t(able|d|h)$/i.test(offsetParent.tagName) || safari && !safari2 )
45                                         border( offsetParent );
46                                         
47                                 // Add the document scroll offsets if position is fixed on any offsetParent
48                                 if ( !fixed && jQuery.css(offsetParent, "position") == "fixed" )
49                                         fixed = true;
50                         
51                                 // Set offsetChild to previous offsetParent unless it is the body element
52                                 offsetChild  = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent;
53                                 // Get next offsetParent
54                                 offsetParent = offsetParent.offsetParent;
55                         }
56                 
57                         // Get parent scroll offsets
58                         while ( parent && parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
59                                 // Remove parent scroll UNLESS that parent is inline or a table to work around Opera inline/table scrollLeft/Top bug
60                                 if ( !/^inline|table.*$/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 <= 2 doubles body offsets with a fixed position element/offsetParent or absolutely positioned offsetChild
73                         // Mozilla doubles body offsets with a non-absolutely positioned offsetChild
74                         if ( (safari2 && (fixed || jQuery.css(offsetChild, "position") == "absolute")) || 
75                                 (mozilla && jQuery.css(offsetChild, "position") != "absolute") )
76                                         add( -doc.body.offsetLeft, -doc.body.offsetTop );
77                         
78                         // Add the document scroll offsets if position is fixed
79                         if ( fixed )
80                                 add(Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
81                                         Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop));
82                 }
83
84                 // Return an object with top and left properties
85                 results = { top: top, left: left };
86         }
87
88         function border(elem) {
89                 add( jQuery.curCSS(elem, "borderLeftWidth", true), jQuery.curCSS(elem, "borderTopWidth", true) );
90         }
91
92         function add(l, t) {
93                 left += parseInt(l) || 0;
94                 top += parseInt(t) || 0;
95         }
96
97         return results;
98 };
99
100 // Create innerHeight, innerWidth, outerHeight and outerWidth methods
101 jQuery.each(["Height", "Width"], function(i, name){
102
103         var tl = name == "Height" ? "Top"    : "Left",  // top or left
104                 br = name == "Height" ? "Bottom" : "Right"; // bottom or right
105         
106         // innerHeight and innerWidth
107         jQuery.fn["inner" + name] = function(){
108                 return this[ name.toLowerCase() ]() + 
109                         num(this, "padding" + tl) + 
110                         num(this, "padding" + br);
111         };
112         
113         // outerHeight and outerWidth
114         jQuery.fn["outer" + name] = function(options) {
115                 options = jQuery.extend({ margin: false }, options);
116                 
117                 return this["inner" + name]() + 
118                         num(this, "border" + tl + "Width") +
119                         num(this, "border" + br + "Width") +
120                         (options.margin ? 
121                                 num(this, "margin" + tl) + num(this, "margin" + br) :
122                                 0);
123         };
124         
125 });
126
127 function num(elem, prop) {
128         elem = elem.jquery ? elem[0] : elem;
129         return elem && parseInt( jQuery.curCSS(elem, prop, true) ) || 0;
130 }