Skip to content

Instantly share code, notes, and snippets.

@rubedell
Created October 14, 2013 14:09
Show Gist options
  • Save rubedell/6976268 to your computer and use it in GitHub Desktop.
Save rubedell/6976268 to your computer and use it in GitHub Desktop.
function equalHeight(element) {
var currentTallest = 0,
currentRowStart = 0,
rowDivs = new Array(),
$el,
topPosition = 0;
$(element).each(function() {
$el = $(this);
topPostion = $el.position().top;
if (currentRowStart != topPostion) {
// we just came to a new row. Set all the heights on the completed row
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
rowDivs[currentDiv].height(currentTallest);
}
// set the variables for the new row
rowDivs.length = 0; // empty the array
currentRowStart = topPostion;
currentTallest = $el.height();
rowDivs.push($el);
} else {
// another div on the current row. Add it to the list and check if it's taller
rowDivs.push($el);
currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
}
// do the last row
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
rowDivs[currentDiv].height(currentTallest);
}
});
}
Copy link

ghost commented Jul 31, 2018

Thanks for the snippet, it helped me a lot. And you have a typo on line 11: topPosition not topPostion

@binzorellino
Copy link

There is a topPostion at in line 13 also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment