Skip to content

Instantly share code, notes, and snippets.

@sardbaba

sardbaba/jquery-outer.js

Forked from tlync/jquery-outer.js
Last active Aug 29, 2015
Embed
What would you like to do?
Setter for jQuery outerWidth/outerHeight, in a compact version
/** Get or set the current outer width/height for the first element in the set of matched elements. */
var origOuterWidth = $.fn.outerWidth;
$.fn.outerWidth = function(){
var value = arguments[0];
if (arguments.length === 0 || typeof value === 'boolean') { return origOuterWidth.apply(this, arguments); }
else if (typeof value !== 'number') { throw new Error('Invalid argument. The new outerWidth value must be an integer.'); }
var css = ['borderLeftWidth','borderRightWidth','paddingLeft','paddingRight'];
if (arguments[1] === true) { css.push('marginLeft'); css.push('marginRight'); }
var $el = $(this), exclude = 0, parse = parseFloat;
for (var i=0; i<css.length; i++) { exclude += parse($el.css(css[i])); }
return $el.width(value - exclude);
};
var origOuterHeight = $.fn.outerHeight;
$.fn.outerHeight = function(){
var value = arguments[0];
if (arguments.length === 0 || typeof value === 'boolean') { return origOuterHeight.apply(this, arguments); }
else if (typeof value !== 'number') { throw new Error('Invalid argument. The new outerHeight value must be an integer.'); }
var css = ['borderTopWidth','borderBottomWidth','paddingTop','paddingBottom'];
if (arguments[1] === true) { css.push('marginTop'); css.push('marginBottom'); }
var $el = $(this), exclude = 0, parse = parseFloat;
for (var i=0; i<css.length; i++) { exclude += parse($el.css(css[i])); }
return $el.height(value - exclude);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.