Last active
November 9, 2022 09:43
-
-
Save LeoSeyers/2748c4fe89e15b507428e7c0bd1c9c51 to your computer and use it in GitHub Desktop.
Simple slide animation
This file contains hidden or 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
export class DOMAnimations { | |
/** | |
* Masque un élément avec un effet de repli | |
* @param {HTMLElement} element | |
* @param {Number} duration | |
* @returns {Promise<boolean>} | |
*/ | |
static slideUp(element, duration = 500) { | |
return new Promise((resolve) => { | |
if (window.getComputedStyle(element).display === 'none') resolve() | |
element.style.height = `${element.offsetHeight}px` | |
element.style.transitionProperty = 'height, margin, padding' | |
element.style.transitionDuration = `${duration}ms` | |
element.offsetHeight // eslint-disable-line no-unused-expressions | |
element.style.overflow = 'hidden' | |
element.style.height = 0 | |
element.style.paddingTop = 0 | |
element.style.paddingBottom = 0 | |
element.style.marginTop = 0 | |
element.style.marginBottom = 0 | |
window.setTimeout(() => { | |
element.style.display = 'none' | |
element.style.removeProperty('height') | |
element.style.removeProperty('padding-top') | |
element.style.removeProperty('padding-bottom') | |
element.style.removeProperty('margin-top') | |
element.style.removeProperty('margin-bottom') | |
element.style.removeProperty('overflow') | |
element.style.removeProperty('transition-duration') | |
element.style.removeProperty('transition-property') | |
resolve(false) | |
}, duration) | |
}) | |
} | |
/** | |
* Affiche un élément avec un effet de dépliement | |
* @param {HTMLElement} element | |
* @param {Number} duration | |
* @returns {Promise<boolean>} | |
*/ | |
static slideDown(element, duration = 500, display = 'block') { | |
return new Promise((resolve) => { | |
element.style.removeProperty('display') | |
// let { display } = window.getComputedStyle(element) | |
// if (display === 'none') display = 'block' | |
element.style.display = display | |
const height = element.offsetHeight | |
element.style.overflow = 'hidden' | |
element.style.height = 0 | |
element.style.paddingTop = 0 | |
element.style.paddingBottom = 0 | |
element.style.marginTop = 0 | |
element.style.marginBottom = 0 | |
element.offsetHeight // eslint-disable-line no-unused-expressions | |
element.style.transitionProperty = 'height, margin, padding' | |
element.style.transitionDuration = `${duration}ms` | |
element.style.height = `${height}px` | |
element.style.removeProperty('padding-top') | |
element.style.removeProperty('padding-bottom') | |
element.style.removeProperty('margin-top') | |
element.style.removeProperty('margin-bottom') | |
window.setTimeout(() => { | |
element.style.removeProperty('height') | |
element.style.removeProperty('overflow') | |
element.style.removeProperty('transition-duration') | |
element.style.removeProperty('transition-property') | |
resolve(false) | |
}, duration) | |
}) | |
} | |
/** | |
* Affiche ou Masque un élément avec un effet de repli | |
* @param {HTMLElement} element | |
* @param {Number} duration | |
* @returns {Promise<boolean>} | |
*/ | |
static slideToggle(element, duration = 500) { | |
if (window.getComputedStyle(element).display === 'none') { | |
return this.slideDown(element, duration) | |
} | |
return this.slideUp(element, duration) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment