Skip to content

Instantly share code, notes, and snippets.

@wesamly
Created March 17, 2020 11:06
Show Gist options
  • Save wesamly/df0acd7c477a803fd6b9e35f95c99b47 to your computer and use it in GitHub Desktop.
Save wesamly/df0acd7c477a803fd6b9e35f95c99b47 to your computer and use it in GitHub Desktop.
Make adjacent divs same height with Javascript
function resetSameTopElmsHeight(container, childSelector) {
var cols, ph, pt, tempElms, i, ch, co, j;
cols = container.find(childSelector);
ph = -1;
pt = -1;
tempElms = [];
for (i in cols) {
ch = $(cols[i]).outerHeight();
co = $(cols[i]).offset();
if (pt != -1 && pt < co.top) {
pt = co.top;
for (j in tempElms) {
$(tempElms[j]).outerHeight(ph)
}
ph = -1
tempElms = [];
}
tempElms.push(cols[i]);
if (ph < ch) {
ph = ch;
}
pt = co.top;
}
}
//Example:
resetSameTopElmsHeight($('.row'), '.col-sm-6');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment