@keyframes
and animation
:if i have an element that has some styles like:
div{
width:20px;
color:purple;
background:white;
}
by default when the page run they will run(or they might work on any event).these styles will keep runing until the point the browser reaches the animation
property:
div{
width:20px;
color:purple;
background:white;
animation:jump 3s 2 3 ease;
}
now the animation
property will start runing and style the element with it's value.in our case jump 3s 2 3 ease
.at that point @keyframes
will begin running it's frames(0%,50%,...):
@keyframes{
0%{
styles
}
50%{
styles
}
100%{
styles
}
you need to know now that the animation
property will run at least one cycle or as specified with the animation-iteration-count
. a cycle with time set using animation-duration
prpoerty(for example 2s or infinite).in our case here the animation will be 3 cycles of animation.that is runing the styles in the frames 3 times each cycle of will take 3 seconds to end at a speed rate of ease.note here that once the animation has finished and no cycles are left to be run again the animation
property will end it's job and we are back to our element's styles defined before and after the animation
property unless we specify animation-iteration-count
property to be infinite
.
the frames will run the code inside it and therefore new styls and updeted styles will be added to the element as an animation.the frames 0%,25%...are points in the time specified for the animation and this is the reason we use css animation using @keyframes
so that we can add styles or update styles at these points as much as we wish.
animation-duration
when controlled using the frames is similar to painting a gradient line (remember the:pure red at 50%!) except that the last time-stop(color-stop in gradients) if specified to be 80% for instance in gradient was the gradient line is painted with that last color-stop value.animation is different as it will take the come-back value (the normal state value assigned to the element before running aniamtion property
) and finish the cycle with it.
code explains the above:
div{ normal state come-back values } @keyframes{ 0%{ if missing,then come-back values will be used until we get to the first frame } 50%{ animation styles } 100%{ if missing,then come-back values will be used until we get to the end of cycle }
it all depends on the animation i have in mind,it is all about the jump from the normal state to the first keyframe and the delay time incase of any.remember not starting with 0%{} will cause the animation to start from the normal state all the way to the first defined keyframe without any jumping.smooth animation.defining two diffrent values in the normal state and the first keyframe (to{}) will cause the jump to happen.having a delay time before the animation is fine because i can use the animation-fill-mode:backwards
value which states that the element will be animated with the first keyframe values during the delay time before the animation starts.hence when it starts it will start with the first keyframe without any evidance of jump.remember thios property works only if we have delay time.the last case is if we assign the same values to both normal state and first keyframe.the animation will start from normal state with no jump.
again remember that not defining the last keyframe 100%{} will get us back to the normal state value.in other words ending the animation with the normal state value.this means there is no jump.because the jump behaviour will take a place if the last frame value is different from the normal state value.that is when the cycle end we will jump back to the normal state so if we assign the same values for both last keyframe and normal state there will be no jumping.also if we use animation-fill-mode:fowards
property value it will ensure that the elelment will finish it's last cycle of animation with the last keyfrmae value(this will not happen if we use infinite
).giving us the chance to assign whatever values for the last keyfrmae and stop at it without any evidance of jump and not worrying about assigning the same values used in the normal state to the last keyframe to avoid the jump behaviour.animation-direction:alternate
value will let us avoid that jump behaviour even when infinite
is used.
transform
) i need to calculate the element size,the container size and how much space is left when the element is transformed.
ease-in
for the frame 50%
.that speed rate will be given for the intermediate values bettwen the 50% frame and the next frame.
animation-direction:
reverse values will reverse easing functions as well!.timing-function
on one of the frmaes along with the timing-function
that is used inside the animation
property.the timing-function used on one frame will affect only that frame duration.i can notice the difference here:
timing-function
used in animation
property