Created
May 18, 2021 18:44
-
-
Save johnloy/1d5904230118a4761261486cfac39291 to your computer and use it in GitHub Desktop.
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
/** | |
* This is a better alternative to using 'forwards' | |
* fill mode, as it allows you to update inline styles | |
* after the animation has completed. Contrast with: | |
* | |
* element.animate({ | |
* { opacity: '0.2' }, | |
* { | |
* duration: 500, | |
* easing: 'ease-in-out', | |
* fill: 'forwards', | |
* }, | |
* }); | |
* | |
* element.style.opacity = '1'; // never applied | |
*/ | |
function animateTo(element, keyframes, options) { | |
const anim = element.animate( | |
keyframes, | |
{ ...options, fill: 'both' }, | |
); | |
anim.addEventListener('finish', () => { | |
anim.commitStyles(); | |
anim.cancel(); | |
}); | |
return anim; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment