Skip to content

Instantly share code, notes, and snippets.

@sambeevors
Created June 5, 2019 13:56
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 sambeevors/10a5e0d1083590f02aa03e2e8c2427a9 to your computer and use it in GitHub Desktop.
Save sambeevors/10a5e0d1083590f02aa03e2e8c2427a9 to your computer and use it in GitHub Desktop.
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