Animations
Learn how your designs can be made more interesting and entertaining by using animation to draw user’s attention to interactive elements.
Learn how your designs can be made more interesting and entertaining by using animation to draw user’s attention to interactive elements.
As mentioned above keyframes are essential to build an animation. But what they are?
Keyframes are the mechanism used to link animation states to timestamps along a timeline in animation software, CSS, and the majority of other tools that let you animate something. In other words, the entire animation spans across at least two states and lasts a set period of time in-between them.
Check out these examples:
/* two oppposite states using 'from' and 'to' */ @keyframes animation-1 { from { background-color: red; } to { background-color: blue; } } /* three states using percentage scale from 0% to 100% */ @keyframes animation-2 { 0% { background-color: red; } 50% { background-color: blue; } 100% { background-color: yellow; } }
As shown above, you can use in-rules, from and to since they are the keywords that represent 0% and 100%, which are the start of the animation and end. Though you can use percentages as well as shown in example of the animation-2.
Since you know how @keyframes work, now it time to move to the animation properties.
Identifies the keyframe animation that should be applied to the element by name.
Let’s see the syntax:
@keyframes zoom { /* declare animation actions here */ } .animated { animation-name: zoom; }
The animation-duration property defines the time frame in which the animation should take place. Both seconds (s) and milliseconds (ms) can be used to specify the time.
Let’s see the syntax:
.animated { animation-duration: 2s; }
Defines the timing mechanism that governs the acceleration and deceleration of the animation. Ease, linear, ease-in, ease-out, and ease-in-out are most commonly used values out there.
See the syntax:
.animated { /* 1 */ animation-timing-function: linear; /* 2 */ animation-timing-function: ease; /* 3 */ animation-timing-function: ease-in; /* 4 */ animation-timing-function: ease-out; /* 5 */ animation-timing-function: ease-in-out; /* 6 */ animation-timing-function: cubic-bezier(0, 0.4, 0.6, 1); /* (x1,y1,x2,y2) where x1 and x2 must be a number from 0 to 1 */ /* 7 */ animation-timing-function: steps(24); /* (n) where n must be a natural number larger or equal to 1 */ }
To determine when an animation will begin, you can use the animation-delay property. By default transitions begin immediately (0s) if not specified otherwise. The time unit accepted by this property is both seconds (s) and milliseconds (ms).
See the syntax:
.animated { animation-delay: 250ms; animation-delay: 0.25ms; }
Please note, that in contrast to the animation-duration property, you can specify a negative value for this. The timeline in your @keyframes will begin there if you specify a negative value. For instance, if you set animation-delay to -1s and your animation lasts for 2 seconds, it will begin at the halfway point of your timeline.
The animation-play-state property simply indicates whether or not the animation is in progress. Running and paused are the alternate values.
See the syntax:
.animated { animation-play-state: running; animation-play-state: paused; }
The animation-iteration-count property specifies how many times the animation should loop. You can repeat something an infinite number of times or a specific number of times.
By default, this is 1, meaning that when the animation goes the end of your timeline, it will stop at the end. The set value cannot be a negative number.
See the syntax:
.animated { /* 1 */ animation-iteration-count: 7; /* will go 7 times */ /* 2 */ animation-iteration-count: 6; /* will go 6 times */ /* 3 */ animation-iteration-count: 5; /* will go 5 times */ /* 4 */ animation-iteration-count: 4; /* will go 4 times */ /* 5 */ animation-iteration-count: 3; /* will go 3 times */ /* 6 */ animation-iteration-count: 2; /* will go 2 times */ /* 7 */ animation-iteration-count: 1; /* will go 1 time */ }
Defines the timing mechanism that governs the acceleration and deceleration of the animation. Ease, linear, ease-in, ease-out, and ease-in-out are most commonly used values out there.
See the syntax:
.animated { /* 1 */ animation-direction: normal; /* 2 */ animation-direction: reverse; /* 3 */ animation-direction: alternate; /* 4 */ animation-direction: alternate-reverse; }
The animation-fill-mode property specifies the styling of the element both before and after the animation. None, forwards, backwards, and both are the possible values.
See the syntax:
.animated { /* 1 */ animation-fill-mode: none; /* 2 */ animation-fill-mode: forwards; /* 3 */ animation-fill-mode: backwords; /* 4 */ animation-fill-mode: both; }
Ok let’s unpack what actually happened in above example. Animation comprises of 4 elements with different classes representing the different animation-fill-mode values. The animation we define is a simple sliding animation that moves the element from left to right.
If needed refresh the page to see all animation effects.
An animation shorthand property allows you to define the animation properties all at once, rather than having to define each one separately in following order:
See the syntax and order of values in an example:
.animated { animation: rotate 500ms cubic-bezier(0.25, 0.1, 0.25, 1) 2s infinite normal none running; }
Before animating any property, you should be aware of possible performance issue on user’s end, especially when he/seh is using an old device.
However, there are safe combinations can be animated without such risk:
MDN Web Docs has a full list of CSS properties which can be animated.
Animatable properties tend to colors and numbers. An example a non-animatable property is background-image.
I use feedback to improve UI Crux Platform. If you noticed a mistake, please report it.