Added some significant speed-ups to height/width checks, thanks to some code and...
[jquery.git] / src / dimensions.js
1 // Create innerHeight, innerWidth, outerHeight and outerWidth methods
2 jQuery.each([ "Height", "Width" ], function(i, name){
3
4         var tl = i ? "Left"  : "Top",  // top or left
5                 br = i ? "Right" : "Bottom", // bottom or right
6                 lower = name.toLowerCase();
7
8         // innerHeight and innerWidth
9         jQuery.fn["inner" + name] = function(){
10                 return this[0] ?
11                         jQuery.css( this[0], lower, false, "padding" ) :
12                         null;
13         };
14
15         // outerHeight and outerWidth
16         jQuery.fn["outer" + name] = function(margin) {
17                 return this[0] ?
18                         jQuery.css( this[0], lower, false, margin ? "margin" : "border" ) :
19                         null;
20         };
21         
22         var type = name.toLowerCase();
23
24         jQuery.fn[ type ] = function( size ) {
25                 // Get window width or height
26                 return this[0] == window ?
27                         // Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
28                         document.compatMode == "CSS1Compat" && document.documentElement[ "client" + name ] ||
29                         document.body[ "client" + name ] :
30
31                         // Get document width or height
32                         this[0] == document ?
33                                 // Either scroll[Width/Height] or offset[Width/Height], whichever is greater
34                                 Math.max(
35                                         document.documentElement["client" + name],
36                                         document.body["scroll" + name], document.documentElement["scroll" + name],
37                                         document.body["offset" + name], document.documentElement["offset" + name]
38                                 ) :
39
40                                 // Get or set width or height on the element
41                                 size === undefined ?
42                                         // Get width or height on the element
43                                         (this.length ? jQuery.css( this[0], type ) : null) :
44
45                                         // Set the width or height on the element (default to pixels if value is unitless)
46                                         this.css( type, typeof size === "string" ? size : size + "px" );
47         };
48
49 });