A Pen by Jordan Cauley on CodePen.
Created
June 4, 2015 15:11
-
-
Save jdcauley/59b8ac6a06fe6ee6e4e3 to your computer and use it in GitHub Desktop.
Height Matching Elements in Vanilla JS
This file contains hidden or 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
<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> |
This file contains hidden or 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
(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; | |
})(); |
This file contains hidden or 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
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