Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tiny JS class to match the height of supplied elements
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
You can’t perform that action at this time.