Skip to content

Instantly share code, notes, and snippets.

@KZeni
Created October 30, 2013 00:26
Show Gist options
  • Save KZeni/7225183 to your computer and use it in GitHub Desktop.
Save KZeni/7225183 to your computer and use it in GitHub Desktop.
Updated jQuery Equalize.js to add option for "margin" to be true or false (false being default). This takes the bottom margin of the last child of the element(s) being equalized and adds that to the resulting height (simply using outerHeight isn't sufficient as that doesn't take the margin of children elements into account). Useful when there's …
;(function($) {
$.fn.equalize = function(options) {
var $containers = this, // this is the jQuery object
children = false,
reset = false,
margin = false,
equalize,
type;
// when options are an object
if ($.isPlainObject(options)) {
equalize = options.equalize || 'height';
margin = options.margin || false;
children = options.children || false;
reset = options.reset || false;
} else { // otherwise, a string was passed in or default to height
equalize = options || 'height';
}
if (!$.isFunction($.fn[equalize])) { return false; }
// determine if the height or width is being equalized
type = (equalize.indexOf('eight') > 0) ? 'height' : 'width';
return $containers.each(function() {
// when children exist, equalize the passed in child elements, otherwise equalize the children
var $children = (children) ? $(this).find(children) : $(this).children(),
max = 0; // reset for each container
$children.each(function() {
var $element = $(this),
value;
if (reset) { $element.css(type, ''); } // remove existing height/width dimension
value = $element[equalize](); // call height(), outerHeight(), etc.
if (margin && type == 'height') { value+= parseInt($element.find('> :last').css('marginBottom').replace('px','')); } // add the bottom margin of the last child of this element to the height
if (value > max) { max = value; } // update max
});
$children.css(type, max +'px'); // add CSS to children
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment