Created
October 29, 2015 19:50
-
-
Save vctrfrnndz/8a5f347fd34d83260408 to your computer and use it in GitHub Desktop.
Calculate equal height based on content
This file contains hidden or 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
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