Last active
August 15, 2019 20:05
-
-
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
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
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