-
-
Save jensgro/c4e890059e96ee48bf025a13522ca0ef to your computer and use it in GitHub Desktop.
slideUp, slideDown, slideToggle
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
// In order for these functions to work, the target element needs a transition CSS property to be set. | |
function slideUp(element, callback) { | |
element.style.overflow = 'hidden'; | |
element.style.height = element.clientHeight + 'px'; | |
setTimeout(() => { | |
element.style.height = 0; | |
}, 0); | |
element.addEventListener('transitionend', () => { | |
element.style.removeProperty('overflow'); | |
element.style.removeProperty('height'); | |
element.style.display = 'none'; | |
callback && callback(); | |
}, { once: true }); | |
} | |
function slideDown(element, callback) { | |
element.style.overflow = 'hidden'; | |
element.style.display = 'block'; | |
element.style.height = 'auto'; | |
let targetHeight = element.clientHeight + 'px'; | |
element.style.height = '0px'; | |
setTimeout(() => { | |
element.style.height = targetHeight; | |
}, 0); | |
element.addEventListener('transitionend', () => { | |
element.style.removeProperty('overflow'); | |
element.style.removeProperty('height'); | |
callback && callback(); | |
}, { once: true }); | |
} | |
function slideToggle(element, callback) { | |
if (getComputedStyle(element).display === 'none') { | |
slideDown(element, callback); | |
} else { | |
slideUp(element, callback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment