-
-
Save jakearchibald/0b50c4918eaf9a67bfcfa55e7e61cd56 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
/** | |
* @param {HTMLElement} element | |
* @param {Keyframe[] | PropertyIndexedKeyframes} to | |
* @param {KeyframeAnimationOptions} options | |
*/ | |
export function animateTo(element, to, options) { | |
const anim = element.animate( | |
to, | |
{ ...options, fill: 'both' }, | |
); | |
anim.addEventListener('finish', () => { | |
anim.commitStyles(); | |
anim.cancel(); | |
}); | |
return anim; | |
} | |
/** | |
* @param {HTMLElement} element | |
* @param {PropertyIndexedKeyframes} from | |
* @param {KeyframeAnimationOptions} options | |
*/ | |
export function animateFrom(element, from, options) { | |
return element.animate( | |
{ ...from, offset: 0 }, | |
{ ...options, fill: 'backwards' }, | |
); | |
} |
Yeah, pseudo-elements currently cannot have inline styles. I hope that changes some day, and it's been discussed. But got now, it's better to use animateFrom
on pseudos.
I see, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
anim.commitStyles()
throws an Error while animating pseudo elements:Possible solution: call
commitStyles()
only if the user is not animating a pseudo-element with:if(!( 'pseudoElement' in options && options['pseudoElement'] )) anim.commitStyles();
This prevents an Error from throwing, but might give users a false sense of security that the pseudo-element will retain the styles after the animation finishes. May be a side note in the JSDoc?