Skip to content

Instantly share code, notes, and snippets.

@wiegertschouten
Last active February 21, 2023 14:13
Show Gist options
  • Save wiegertschouten/3eea26d802a2d3ef429d6ca17f37fbf5 to your computer and use it in GitHub Desktop.
Save wiegertschouten/3eea26d802a2d3ef429d6ca17f37fbf5 to your computer and use it in GitHub Desktop.
slideUp, slideDown, slideToggle
// 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