Added in .height(fn) and .width(fn) support. Fixes #5915.
[jquery.git] / src / dimensions.js
1 // Create innerHeight, innerWidth, outerHeight and outerWidth methods
2 jQuery.each([ "Height", "Width" ], function( i, name ) {
3
4         var type = name.toLowerCase();
5
6         // innerHeight and innerWidth
7         jQuery.fn["inner" + name] = function() {
8                 return this[0] ?
9                         jQuery.css( this[0], type, false, "padding" ) :
10                         null;
11         };
12
13         // outerHeight and outerWidth
14         jQuery.fn["outer" + name] = function( margin ) {
15                 return this[0] ?
16                         jQuery.css( this[0], type, false, margin ? "margin" : "border" ) :
17                         null;
18         };
19
20         jQuery.fn[ type ] = function( size ) {
21                 // Get window width or height
22                 var elem = this[0];
23                 if ( !elem ) {
24                         return size == null ? null : this;
25                 }
26                 
27                 if ( jQuery.isFunction( size ) ) {
28                         return this.each(function( i ) {
29                                 var self = jQuery( this );
30                                 self[ type ]( size.call( this, i, self[ type ]() ) );
31                         });
32                 }
33
34                 return ("scrollTo" in elem && elem.document) ? // does it walk and quack like a window?
35                         // Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
36                         elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] ||
37                         elem.document.body[ "client" + name ] :
38
39                         // Get document width or height
40                         (elem.nodeType === 9) ? // is it a document
41                                 // Either scroll[Width/Height] or offset[Width/Height], whichever is greater
42                                 Math.max(
43                                         elem.documentElement["client" + name],
44                                         elem.body["scroll" + name], elem.documentElement["scroll" + name],
45                                         elem.body["offset" + name], elem.documentElement["offset" + name]
46                                 ) :
47
48                                 // Get or set width or height on the element
49                                 size === undefined ?
50                                         // Get width or height on the element
51                                         jQuery.css( elem, type ) :
52
53                                         // Set the width or height on the element (default to pixels if value is unitless)
54                                         this.css( type, typeof size === "string" ? size : size + "px" );
55         };
56
57 });