Skip to content

Instantly share code, notes, and snippets.

@krillo
Last active April 13, 2017 13:48
Show Gist options
  • Save krillo/4ad1bfc4f8f46052e10d77a9aa7fc6a0 to your computer and use it in GitHub Desktop.
Save krillo/4ad1bfc4f8f46052e10d77a9aa7fc6a0 to your computer and use it in GitHub Desktop.
jQuery: find min height of elements
<script type="text/javascript">
jQuery(document).ready(function ($) {
var minHeight = 0;
$('.macro-env-images li img').each(function (index) {
height = $(this).height();
console.log(index + ": " + $(this).height());
if (index === 0) {
minHeight = height;
}
if (height > 0 && height < minHeight) {
minHeight = height;
}
});
console.log('minHeight ' + minHeight);
$('.macro-env-images li').css('height', minHeight);
});
//alternative
// delay to wait for images to load
// use array and Math min
// min height is sety to 30 which is higher than the element that shows when an image is not found, 404
jQuery(document).ready(function ($) {
var minHeight = 0;
var heights = new Array();
setTimeout(function () {
setImageHeights();
}, 250);
function setImageHeights() {
$('.macro-env-images li a img').each(function (index) {
height = $(this).height();
//console.log(index + ": " + height);
if (height > 30) {
heights.push(height);
}
});
minHeight = Math.min.apply(Math, heights);
//console.log('minHeight ' + minHeight);
$('.macro-env-images li').css('height', minHeight);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment