aboutsummaryrefslogtreecommitdiffstats
path: root/src/dimensions.js
blob: 79d7c22bc2138e1ec6799c7db9f25a92ec7f0af5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(function( jQuery ) {

// Create width, height, innerHeight, innerWidth, outerHeight and outerWidth methods
jQuery.each( { Height: "height", Width: "width" }, function( name, type ) {
	var clientProp = "client" + name,
		scrollProp = "scroll" + name,
		offsetProp = "offset" + name;

	// height, width, innerHeight and innerWidth
	jQuery.each( { padding: "inner" + name, content: type }, function( extra, funcName ) {
		jQuery.fn[ funcName ] = function( value ) {
			var args = [ type, extra ];
			if ( arguments.length ) {
				args.push( value );
			}
			return getDimension.apply( this, args );
		};
	});

	// outerHeight and outerWidth
	jQuery.fn[ "outer" + name ] = function( margin, value ) {
		var args = [ type, ( margin === true || value === true ) ? "margin" : "border" ];
		if ( arguments.length && typeof margin !== "boolean" ) {
			args.push( margin );
		}
		return getDimension.apply( this, args );
	};

	function getDimension( type, extra, value ) {
		return jQuery.access( this, function( elem, type, value ) {
			var doc, orig, ret;

			if ( jQuery.isWindow( elem ) ) {
				// As of 5/8/2012 this will yield incorrect results for Mobile Safari, but there
				// isn't a whole lot we can do. See pull request at this URL for discussion:
				// https://github.com/jquery/jquery/pull/764
				return elem.document.documentElement[ clientProp ];
			}

			// Get document width or height
			if ( elem.nodeType === 9 ) {
				// Either scroll[Width/Height] or offset[Width/Height], whichever is greater
				doc = elem.documentElement;

				// when a window > document, IE6 reports a offset[Width/Height] > client[Width/Height]
				// so we can't use max, as it'll choose the incorrect offset[Width/Height]
				// instead we use the correct client[Width/Height]
				// support:IE6
				if ( doc[ clientProp ] >= doc[ scrollProp ] ) {
					return doc[ clientProp ];
				}

				return Math.max(
					elem.body[ scrollProp ], doc[ scrollProp ],
					elem.body[ offsetProp ], doc[ offsetProp ]
				);
			}

			// Get width or height on the element
			if ( value === undefined ) {
				orig = jQuery.css( elem, type, extra );
				ret = parseFloat( orig );
				return jQuery.isNumeric( ret ) ? ret : orig;
			}

			// Set the width or height on the element
			jQuery.style( elem, type, value, extra );
		}, type, value, arguments.length > 2, null );
	}
});

})( jQuery );