Skip to content

Instantly share code, notes, and snippets.

@vctrfrnndz
Created October 29, 2015 19:50
Show Gist options
  • Save vctrfrnndz/8a5f347fd34d83260408 to your computer and use it in GitHub Desktop.
Save vctrfrnndz/8a5f347fd34d83260408 to your computer and use it in GitHub Desktop.
Calculate equal height based on content
function heightEqualizer($row) {
var $columns = $row.find('> *');
function calculateHeight($el) {
var height = 0;
$el.each(function() {
var heightVal = $(this).css('height');
$(this).css('height', '');
height = height + $(this).outerHeight(true);
$(this).css('height', heightVal);
});
return height;
}
function calculateMaxHeight($elems) {
var maxHeight = 0;
$elems.each(function() {
var currentHeight = calculateHeight($(this));
if (currentHeight > maxHeight) {
maxHeight = currentHeight;
}
});
return maxHeight;
}
function setMaxHeight($elems) {
$elems.css('height', calculateMaxHeight($elems) + 'px');
}
setMaxHeight($columns);
if ($row.find('img').length) {
var imgLoaded = 0;
$row.find('img').each(function() {
$(this).on('load', function() {
imgLoaded++
if (imgLoaded === $row.find('img').length) {
setMaxHeight($columns);
}
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment