Skip to content

Instantly share code, notes, and snippets.

@johnhunter
Created October 27, 2010 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnhunter/649856 to your computer and use it in GitHub Desktop.
Save johnhunter/649856 to your computer and use it in GitHub Desktop.
jQuery plugin that ensures html elements share the same height
/*
jQuery.equalHeights Plugin
@author John Hunter
@dependency jQuery 1.4.x
EqualHeights version 2.0 (2010-09-07 johnhunter.info)
Based on an idea copyright (c) 2008 Rob Glazebrook (cssnewbie.com)
use: $('#selector').equalHeights({minHeight: 200});
see defaults object for options
*/
(function($) {
var defaults = {
minHeight: 0,
maxHeight: 0,
overflow: 'auto',
useMinHeight: true,
eachCallback: null
};
$.fn.equalHeights = function (options) {
var tallest,
ohOpts = { margin: true },
heights = this.outerHeights({ margin: true });
options = $.extend({}, defaults, options);
heights.push(options.minHeight);
tallest = Math.max.apply(null, heights);
if(options.maxHeight && tallest > maxHeight) tallest = maxHeight;
return this.each(function(i) {
var el = $(this),
of = options.overflow,
cb = options.eachCallback,
property = (!options.useMinHeight || (/MSIE 6/.test(navigator.userAgent))) ? 'height' : 'min-height',
height = tallest + el.height() - el.outerHeight(ohOpts);
el.css(property, height);
if (of) el.css('overflow', of);
if ($.isFunction(cb)) cb.call(this, i, el, height);
});
};
$.fn.outerHeights = function (opts) {
return $.map(this, function (i) {
return $(i).outerHeight(opts);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment