Created
May 3, 2023 05:01
-
-
Save jxxe/7dac5d57a85c9ace2051d2051ffda0bf to your computer and use it in GitHub Desktop.
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
type SmoothAnimationOptions = { | |
condition: boolean, | |
addClass?: (node: HTMLElement) => void, | |
removeClass?: (node: HTMLElement) => void, | |
animationClass?: string | |
} | |
export function smoothAnimation(node: HTMLElement, { condition, addClass, removeClass, animationClass }: SmoothAnimationOptions) { | |
if(animationClass) { | |
if(!removeClass) removeClass = () => node.classList.remove(animationClass); | |
if(!addClass) addClass = () => node.classList.add(animationClass); | |
} | |
function onFinish() { | |
removeClass(node); | |
setTimeout(() => condition && addClass(node)); | |
} | |
node.addEventListener('animationend', onFinish); | |
node.addEventListener('animationiteration', onFinish); | |
return { | |
update(newOptions: SmoothAnimationOptions) { | |
condition = newOptions.condition; | |
if(condition) addClass(node); | |
}, | |
destroy() { | |
removeEventListener('animationend', onFinish); | |
removeEventListener('animationiteration', onFinish); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you remove a class controlling a CSS animation, the element jumps back to its original state without a transition. This Svelte
use
directive makes it easy to smoothly end discrete or continuous CSS animations. It "rounds" animations up to the nearest iteration.Usage