I want to create an animation in css and then link in to javascript,
like when pressing a button to run the css animation
lets say you have:
animation-name: vs-animation
and @keyframes
with the same name
and i would like it to link to startNewRound() = true
, how would i do that
2
Answers
To create a CSS animation linked to JavaScript when clicking a button, you can achieve this by using JavaScript to add and remove CSS classes containing the animation. Here’s how to do it step by step:
@keyframes
. For example:startNewRound()
function when clicked:startNewRound()
function:In this example, we are adding the
vs-animation
class to the element you want to animate when the button is clicked. Then, we are removing that class after a specified time so that the animation can be restarted by clicking again.Make sure to adjust the time in
setTimeout
to match the duration of your animation in milliseconds.Finally, ensure that the element you want to animate has the
vs-animation
class initially so that the animation is ready to run. anthoncode.comWeb Animations API
I recommend the modern way, which would be to use the Web Animations API: The
element.animate()
method allows you to specify keyframes and options.Starting a new animation cancels the previous automatically.
Example (playing a new animation per click):
Alternative example (restarting one animation on click):
Here is an example of how you could get a
@keyframes
at-rule’s keyframes using the CSSOM API:Note: It should work as intended for most cases, but I am not fully certain that it works for every case.
CSS Animations
Alternatively, you can use CSS
@keyframes
at-rules to specify the animation’s name and keyframes.Playing the animation requires at least the name and a duration. These can be given with the
animation
shorthand property, or the respective individual properties.Example:
Note:
Web Animations API uses
"linear"
for theeasing
option by default.CSS animations use
ease
foranimation-timing-function
by default.Unfortunately, to enable replay we cannot simply remove then add the animation-related declarations. Before adding them back, we have to wait for a reflow; for the browser to check for CSS changes.
Example of simply removing then adding (which does not replay the animation):
Changes to the CSS will automatically cause a reflow before a frame, but after JS. That means waiting for one frame before re-adding the declarations will work: See MDN’s Run an animation again example.
Note: Generally, we have to wait for a reflow (the "layout engine"), not repaint (the "rendering engine"). @AdamLeggett comments that "
requestAnimationFrame
waits for the rendering engine. [Prefer theanimate
function]; the second most technically correct is unfortunatelyvoid element.offsetWidth
."The
void element.offsetWidth
trick is shown below. But again, it is best to use the Web Animations API.But we can cause a reflow manually as well; here is a list of JS-related reflow causes. As the link mentions:
That means, by reading
offsetWidth
(a layout metric) we cause a reflow. The statement is often seen withvoid
to discard the value, to make it conform to strict mode[Can’t find citation again].Ideally, you wouldn’t manually reflow (too often), especially not in time-sensitive applications. However, it does make for arguably simpler (though more "magical") code:
The Web animations API is meant for controlling animations via JS; fitting for your use case. Unfortunately, it doesn’t (yet?) offer a way to convert a
@keyframes
at-rule to the keyframe format natively.In contrast, with the approach relying on CSS animations you have to watch out for caveats (waiting for a reflow inbetween) and technicalities (
requestAnimationFrame
waiting for render, not reflow), and have to use workarounds (reading layout metrics).