Skip to content

Instantly share code, notes, and snippets.

@jdcauley
Created June 4, 2015 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdcauley/59b8ac6a06fe6ee6e4e3 to your computer and use it in GitHub Desktop.
Save jdcauley/59b8ac6a06fe6ee6e4e3 to your computer and use it in GitHub Desktop.
Height Matching Elements in Vanilla JS
<ul class="sample-class">
<li>thing</li>
<li>thing</li>
<li>thing</li>
<li>thing</li>
<li>thing</li>
<li>thing</li>
<li>thing</li>
<li>thing</li>
<li>thing</li>
</ul>
<ul class="sample-class">
<li>thing</li>
<li>thing</li>
<li>thing</li>
<li>thing</li>
<li>thing</li>
</ul>
(function(){
function matchHeight(elementClass){
var sortableList = document.getElementsByClassName(elementClass);
if(sortableList){
var heights = [];
for(var i = 0, x = sortableList.length; i < x; i++){
if(sortableList[i].style && sortableList[i].style.height){
sortableList[i].style.height = '';
}
heights.push(sortableList[i].clientHeight);
};
var max = Math.max.apply( Math, heights );
console.log(max);
for(var j = 0, y = sortableList.length; j < y; j++){
sortableList[j].style.height = max + 'px';
console.log(sortableList[j]);
}
};
}
matchHeight('sample-class');
function run(){
matchHeight('sample-class');
}
window.onresize = run;
})();
ul{
height: auto;
width: 80px;
background: gray;
padding: 40px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment