Skip to content

Instantly share code, notes, and snippets.

@sardbaba
Forked from tlync/jquery-outer.js
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sardbaba/c78396a538bf826f5ffa to your computer and use it in GitHub Desktop.
Save sardbaba/c78396a538bf826f5ffa to your computer and use it in GitHub Desktop.
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