Created
June 5, 2019 13:56
-
-
Save sambeevors/10a5e0d1083590f02aa03e2e8c2427a9 to your computer and use it in GitHub Desktop.
Tiny JS class to match the height of supplied elements
This file contains 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
class HeightGroup { | |
constructor(els) { | |
this.nodeArray = [...els] | |
this.nodeHeights | |
this.newHeight | |
} | |
matchHeights() { | |
this.nodeHeights = [] | |
this.newHeight = null | |
this.nodeArray.forEach(node => { | |
node.style.minHeight = '' | |
this.nodeHeights.push(node.offsetHeight) | |
}) | |
this.newHeight = Math.max.apply(null, this.nodeHeights) | |
this.nodeArray.forEach(node => { | |
node.style.minHeight = `${this.newHeight}px` | |
}) | |
} | |
watchElements() { | |
window.addEventListener('resize', this.matchHeights) | |
this.matchHeights() | |
} | |
destroy() { | |
window.removeEventListener('resize', this.matchHeights) | |
this.nodeArray.forEach(node => { | |
node.style.minHeight = '' | |
}) | |
} | |
} | |
export default HeightGroup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment