Skip to content

Instantly share code, notes, and snippets.

@johnloy
Created May 18, 2021 18:44
Show Gist options
  • Save johnloy/1d5904230118a4761261486cfac39291 to your computer and use it in GitHub Desktop.
Save johnloy/1d5904230118a4761261486cfac39291 to your computer and use it in GitHub Desktop.
/**
* 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