I have a chained CSS animation that's mostly working fine:
#product {
background: red;
width: 10%;
height: 10%;
border: none;
left: -22%;
top: 50%;
top: 40%;
animation: product_across 2s linear, product_down 1s;
animation-delay: 0s, 2s;
}
@-moz-keyframes product_across {
from { left: -22%; }
to { left: 98%; }
}
@-moz-keyframes product_down {
from { top: 40%; left: 98%; }
to { top: 98%; left: 128.5%; }
}
The thing is, after the first animation (product_across
) has finished, I want to apply a style. Not animate to it, just apply it.
Does CSS3 allow for this? I guess I'm looking for a sort of "on animation end" property, or something.
For the moment I'm getting round it with:
@-moz-keyframes product_down {
from { -moz-transform: rotate(45deg); top: 40%; left: 98%; }
to { -moz-transform: rotate(45deg); top: 98%; left: 128.5%; }
}
...where the rotation is the style I wish to apply. By setting the from
/to
states to the same value for this property, it effectively does what I want, but I can't help thinking there must be a better way.
CSS allows animation of HTML elements without using JavaScript or Flash! In this chapter you will learn about the following properties: @keyframes.
Most basic animations can be created with either CSS or JavaScript, but the amount of effort and time differs (see also CSS vs JavaScript Performance). Each has its pros and cons, but these are good guidelines: Use CSS when you have smaller, self-contained states for UI elements.
Using animation-delay . animation: a, b; animation-duration: 2s, 2s; animation-delay: 0s, 4s; The animation b will start after 4s while animation a will start without any delay.
In summary, we should always try to create our animations using CSS transitions/animations where possible. If your animations are really complex, you may have to rely on JavaScript-based animations instead.
You do need to use animation-fill-mode:forwards
as Devin suggests, but this only works for properties set in the animation. In order to set properties other than the ones you want actually animated, you must make them part of the animation by adding them to the very end like so:
@keyframes {
/* ... Your original keyframes (except the last) ... */
99.99% { /* ... The properties of something you want to change ... */ }
100% { /*... Your original end properties and the changed properties ...*/ }
}
for example:
@keyframes rotate {
0% { transform:rotate(0deg); }
99.999% { background:blue; }
100% { transform:rotate(360deg); background:red; }
}
By having the difference between the two end keyframes as really small, it will jump to the new styles no matter what the animation-duration
is.
Demo
The other, more common, way to get this effect is to use Javascript's animation-end
yes, you need to use Animation Fill Mode.
Simply define that style as the last keyframe and then you add this to #product
#product{-webkit-animation-fill-mode:forwards; animation-fill-mode:forwards;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With