So, my CSS moves a div to -40px after 3 seconds. After doing so, the div is then moved back to its original position. Here is the code:
#jsAlert {
width: 100%;
height: 40px;
position: absolute;
left: 0px;
top: 0px;
background-color: rgba(0, 0, 0, 0.6);
animation:mymove 3s;
animation-delay: 3s;
/*Safari and Chrome*/
-webkit-animation: mymove 2s;
-webkit-animation-delay: 2s;
}
@keyframes mymove {
from {top: 0px;}
to {top: -40px;}
}
@-webkit-keyframes mymove /*Safari and Chrome*/
{
from {top: 0px;}
to {top: -40px;}
}
How would I prevent the div from returning to its original position?
You need to use animation-fill-mode
with a value set to forwards
From Mozilla Developer Network:
The target will retain the computed values set by the last keyframe encountered during execution. The last keyframe encountered depends on the value of
animation-direction
andanimation-iteration-count
Demo
i would not recommend using @keyframes for this simple animation.
this article may help you
http://www.kirupa.com/html5/css3_animations_vs_transitions.htm
Conclusion There you have it - a candid look at what makes transitions and animations similar yet so very different. My general approach for determining when to use which goes like this:
If what I want requires the flexibility provided by having multiple keyframes and easy looping, then I go with an animation. If I am looking for a simple from/to animation, I go with a transition. If I want to manipulate the property values that I wish to animate using JavaScript, I go with a transition.
cause performance is a valuable thing .
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