Skip to content

Instantly share code, notes, and snippets.

@willbroderick
Last active November 17, 2015 09:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willbroderick/7f464e6ba26206f662b5 to your computer and use it in GitHub Desktop.
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.
$(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