I have a CSS3 animation that needs to be restarted on a click. It's a bar showing how much time is left. I'm using the scaleY(0) transform to create the effect.
Now I need to restart the animation by restoring the bar to scaleY(1) and let it go to scaleY(0) again. My first attempt to set scaleY(1) failed because it takes the same 15 seconds to bring it back to full length. Even if I change the duration to 0.1 second, I would need to delay or chain the assignment of scaleY(0) to let the bar replenishment complete. It feels too complicated for such a simple task.
I also found an interesting tip to restart the animation by removing the element from the document, and then re-inserting a clone of it: http://css-tricks.com/restart-css-animation/
It works, but is there a better way to restart a CSS animation? I'm using Prototype and Move.js, but I'm not restricted to them.
Just set the
animation
property via JavaScript to"none"
and then set a timeout that changes the property to""
, so it inherits from the CSS again.Demo for Webkit here: http://jsfiddle.net/leaverou/xK6sa/ However, keep in mind that in real world usage, you should also include -moz- (at least).
animationend
event handler function to remove the descriptor when the animation completes so that it will be ready to be added again next time you want to restart the animation.---HTML---
---CSS---
Try it here:
https://jsfiddle.net/bigjosh/avp9Lk6r/50/
If you have some class for css3 animation, for exapmle .blink then you can removeClass for some element and addClass for this element thought setTimeout with 1 milisecond by click.
on this page you can read about restart the element animation: https://css-tricks.com/restart-css-animation/ here is my example:
but if you want to you can just remove the element animation and then return it:
hope i helped!
Create a second "keyframe@" which restarts you animation, only problem with this you cannot set any animation properties for the restarting animation (it just kinda pops back)
----- HTML -----
----- CSS -----
No need in timeout, use reflow to apply the change: