Skip to main content

Bug Tracker

Side navigation

Ticket #4276: dimension_r6195.js
File dimension_r6195.js, 1.6 KB (added by mike.helgeson, March 02, 2009 03:24AM UTC)
// Create innerHeight, innerWidth, outerHeight and outerWidth methods
jQuery.each([ "Height", "Width" ], function(i, name){

	var lower = name.toLowerCase();

	// innerHeight and innerWidth
	jQuery.fn["inner" + name] = function(){
		return this[0] ?
			jQuery.css( this[0], lower, false, "padding" ) :
			null;
	};

	// outerHeight and outerWidth
	jQuery.fn["outer" + name] = function(margin) {
		return this[0] ?
			jQuery.css( this[0], lower, false, margin ? "margin" : "border" ) :
			null;
	};
	
	jQuery.fn[ lower ] = function( size ) {
		// Get window width or height
		return this[0] == window ?
			// Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
			document.compatMode == "CSS1Compat" && document.documentElement[ "client" + name ] ||
			document.body[ "client" + name ] :

			// Get document width or height
			this[0] == document ?
				// Either scroll[Width/Height] or offset[Width/Height], whichever is greater
				Math.max(
					document.documentElement["client" + name],
					document.body["scroll" + name], document.documentElement["scroll" + name],
					document.body["offset" + name], document.documentElement["offset" + name]
				) :

				// Get or set width or height on the element
				size === undefined || { padding:1, border:1, margin:1 }[ size ] ?
					// Get width or height on the element
					(this.length ? jQuery.css( this[0], lower, false, size ) : null) :

					// Set the width or height on the element (default to pixels if value is unitless)
					this.css( lower, typeof size === "string" ? size : size + "px" );
	};

});

Download in other formats:

Original Format