/*! * jquery even heights plugin * author: glen cheney * modified: 2014-03-08 * dependencies: jquery 1.2.6+ * sets a jquery collection to all be the same height * if you need to set multiple collection, please use `$.evenheights( collectionsarray )` * because it is much faster */ (function( $ ) { 'use strict'; function gettallest( $elements ) { var tallest = 0; $elements.each(function() { if ( this.offsetheight > tallest ) { tallest = this.offsetheight; } }); return tallest; } $.fn.evenheights = function() { return this.css( 'height', '' ).css( 'height', gettallest( this ) ); }; /** * for groups of elements which should be the same height. using this method * will create far less style recalculations and layouts. * @param {array.} groups array of jquery collections. * @return {array.} an array containing the pixel value of the * tallest element for each group. */ $.evenheights = function( groups ) { var winners = []; // first, reset the height for every element. // this is done first, otherwise we dirty the dom on each loop! $.each(groups, function( i, $elements ) { $elements.css( 'height', '' ); }); // now, measure heights in each group and save the tallest value. instead of // setting the height value for the entire group, save it. if it were set, // the next iteration in the loop would have to recalculate styles in the dom $.each(groups, function( i, $elements ) { winners.push( gettallest( $elements ) ); }); // lastly, set them all. $.each(groups, function( i, $elements ) { $elements.css( 'height', winners[ i ] ); }); return winners; }; })(window.$);