Skip to content

Instantly share code, notes, and snippets.

@jonnymaceachern
Last active August 15, 2019 20:05
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 jonnymaceachern/d3a1b55fe05f44a38d635de0b834bf67 to your computer and use it in GitHub Desktop.
Save jonnymaceachern/d3a1b55fe05f44a38d635de0b834bf67 to your computer and use it in GitHub Desktop.
Get elements that begin with a common class prefix, group them accordingly, and equalize their heights to their max height
var cousins = [];
// Find all elements that begin with class and return just that matching class
$('[class^=equalize-').each(function(index, el, array) {
var group = $(el).attr('class').match(/\bequalize-[\S]*/);
if ( group.length ) {
cousins.push( group[0] );
}
});
// Make list of matched element classes unique
cousins = cousins.filter(onlyUnique);
// Equalize elements
cousins.forEach(function(groupName){
equalize(groupName);
$(window).on('resize', function(){
equalize(groupName);
});
});
function equalize(groupName) {
var maxHeight = 0;
$('.' + groupName).each(function(){
if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
});
}
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment