Last active
November 17, 2015 09:29
-
-
Save willbroderick/7f464e6ba26206f662b5 to your computer and use it in GitHub Desktop.
Help normalise the heights of items laid out in responsive rows - relies on presence of an inner div that will keep natural height of objects inside. Useful for keeping prices aligned under product images.
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
$(window).on('debouncedresize load', function(){ | |
$('[data-normheights]').each(function(){ | |
var $items = $(this).find($(this).data('normheights')), | |
childFilter = $(this).data('normheights-inner'), | |
tallest = 0, | |
lastYOffset = 0, | |
row = []; | |
$items.each(function(index){ | |
var $img = $(this).find(childFilter); | |
var yOffset = $(this).offset().top; | |
if(index == 0) { | |
lastYOffset = yOffset; | |
} else if(yOffset != lastYOffset) { | |
$(row).css('min-height', tallest); | |
yOffset = $(this).offset().top; | |
row.length = 0; | |
tallest = 0; | |
} | |
lastYOffset = yOffset; | |
row.push(this); | |
var h = $img.height(); | |
if(h > tallest) tallest = h; | |
}); | |
$(row).css('min-height', tallest); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment