Created
July 24, 2012 13:56
-
-
Save byrichardpowell/3170027 to your computer and use it in GitHub Desktop.
jQuery matchHeight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adds a matchHeight method to jQuery/zepto. | |
// Takes a jquery/zepto collection and makes them all as tall as the tallest elements | |
// Ideal for equal column layouts | |
// ## Paramaters | |
// * None (run on a zepto collection instead) | |
// ## Returns | |
// The collection which it was run on | |
// ## Example | |
// $('#myDiv1, #myDiv2').matchHeight(); | |
// #test | |
( function($) { | |
$.fn.matchHeight = function() { | |
if ( this.length ) { | |
var $tallest_element; | |
var tallest_height = 0; | |
/* Loop through each element */ | |
this.each( function() { | |
var $el = $(this); | |
var el_height = $el.outerHeight(); | |
/* This element is taller than the previously tallest element */ | |
if ( el_height > tallest_height ) { | |
/* So store its details */ | |
$tallest_element = $el; | |
tallest_height = el_height; | |
} | |
}); | |
this.not( $tallest_element ).height( tallest_height ); | |
} | |
return this; | |
}; | |
}($)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment