Im trying to get a header to fly in and after that when you hover it, it should shake (both with css3 animation). It flies in the way i want, also shakes, but after ive removed the mouse from the element it goes back to the original margin-right (it had before the flyin animation) even though ive set `-animation-fill-mode: forwards; When i look in chromedevtools the element never changes its margin-right (even though the animation works..). Can i fix this?
Also, is there a way of preventing the first animation to happen again after the shake animation?
flyin animation:
#name {
margin:40px 2% 40px 0;
-webkit-animation:flyin 1.5s;
-webkit-animation-fill-mode: forwards;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-delay: 1800ms;
}
@-webkit-keyframes flyin {
from{margin-right: 2%;}
30% {margin-right: 12%;}
50% {margin-right: 9%;}
60% {margin-right: 10%;}
to {margin-right: 10%;}
}
shake animation:
#name:hover {
**margin-right: 10%; //i also have to set this?! or it starts at 2%**
-webkit-animation:shake 0.7s;
-webkit-animation-fill-mode: forwards;
-webkit-transform-origin:50% 50%;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
}
@-webkit-keyframes shake {
0% { -webkit-transform: translate(2px, 1px) rotate(0deg); }
10% { -webkit-transform: translate(-1px, -2px) rotate(-1deg); }
20% { -webkit-transform: translate(-3px, 0px) rotate(1deg); }
30% { -webkit-transform: translate(0px, 2px) rotate(0deg); }
40% { -webkit-transform: translate(1px, -1px) rotate(1deg); }
50% { -webkit-transform: translate(-1px, 2px) rotate(-1deg); }
60% { -webkit-transform: translate(-3px, 1px) rotate(0deg); }
70% { -webkit-transform: translate(2px, 1px) rotate(-1deg); }
80% { -webkit-transform: translate(-1px, -1px) rotate(1deg); }
90% { -webkit-transform: translate(2px, 2px) rotate(0deg); }
100% { -webkit-transform: translate(2px, 1px) rotate(0deg); }
}
The animation-fill-mode property specifies a style for the element when the animation is not playing (before it starts, after it ends, or both). CSS animations do not affect the element before the first keyframe is played or after the last keyframe is played. The animation-fill-mode property can override this behavior.
CSS animations work on most modern mobile and desktop browsers. However, your animations may not work if you're using an older browser or a version of your browser that hasn't been updated in several years, simply due to lack of browser support.
animation-fill-mode: both applies the rules of both backwards and forwards: The element will adopt the styles of the first keyframe before the animation begins, and the styles of the last keyframe after the animation ends.
The CSS animation-delay property has the following syntax: animation-delay: [time] | initial | inherit; As you can see, there are three possible values: time, initial, and inherit. The first option is [time], which is the number of seconds or milliseconds before the animation starts.
Setting animation-fill-mode: forwards
means that after the animation has completed execution, the animation will hold at final properties until it is removed. When the mouse stops hovering, the -webkit-animation
property returns to its default value (blank), which means that the shake
animation is removed, and everything returns to how it was. To make the animation hold its final properties, you have to keep the shake
animation applied to the element. (In other words, animation-fill-mode
is effective only as long as the animation is applied.)
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