30
loading...
This website collects cookies to deliver better user experience
a method in which figures are manipulated to appear as moving.
transition
and animation
.transition
property allows to define a... well, transition for a CSS property. this transition starts at the initial value (or state) of that property, and ends at the final value. The transition is triggered as soon as the value of that property changes.transition
property is made of 3 required values and 1 optional value.div {
transition: transition-property duration timing-function (delay?);
}
transform
instead of height
? that's a good question! The reason for that is that there are performance concerns when using properties like height
. And the reason for that comes down to layout. Properties like height
cause layout shifting, and therefore, layout recalculations; which can be very expensive.color
, width
, or transform
. It can also be all
to encompass all possible properties, but I would generally advise against this for the sake of predictability, clarity, and performance.s
or milliseconds ms
.cubic-bezier
, A thing I know very little about. thankfully there are tools online that allow you to make and visualize these functions. CSS also includes five ones out-of-the-box! they're linear
, ease
, ease-in
, ease-out
, and ease-in-out
; and they should enough for 90% of your animation needs.s
or milliseconds ms
.active
class to our navbar when the user clicks on the button. Like so:.navbar {
transform: scaleY(0);
}
.navbar.active {
transform: scaleY(1);
}
.navbar-list {
transform: scaleY(0);
transform-origin: top;
transition: transform 250ms ease-out;
}
.navbar.active .navbar-list {
transform: scaleY(1);
}
animation
defines a... you guessed it! animation. And it works largely in the same way as transition
. But there a few key differences.transition
, which triggers when a property updates; animation
is active from the moment it's applied.transition-property
, animation
takes something called keyframes. and we will discuss those soon.animation
property is made of 3 required values and 5 optional valusdiv {
animation: animation-name duration timing-function (delay?) (iteration-count?) (direction?) (fill-mode?) (play-state?);
}
A key frame (or keyframe) in animation and film-making is a drawing or shot that defines the starting and ending points of any smooth transition. These are called frames because their position in time is measured in frames on a strip of film or on a digital video editing timeline. A sequence of key frames defines which movement the viewer will see, whereas the position of the key frames on the film, video, or animation defines the timing of the movement.
@keyframes
at-rule. and is a collection of CSS properties associated with specific points in the animation timeline, like 25%
from
, or to
.from
is the equivalent of 0%
to
is the equivalent of 100%
0%
or from
). whatever state they were in before the animation starts will be their starting state.@keyframes spin {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
spin
keyframes dictates that the element will start at 0 degrees of rotation of rotation, and end at a full 360 degrees.infinite
to make it repeat indefinitely.normal
means that the animation moves normally (from start to end). reverse
means that the animation moves backwards (from end to start). alternate
means that the animation moves forwards one time, and backwards the other time; keep in mind this needs an iteration count higher than 1. and alternate-reverse
is like alternate, but starts as reverse.running
and paused
. pretty self explanatory.spin
keyframes we made earlier we can make a loading spinner! like so:@keyframes spin {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
.spinner {
animation: spin 1s linear infinite;
}
Be careful, when adding your animations. You may think that super flashy, 3 second long animation is cool, but your users might disagree.
Make sure your animations are smooth on all of your target devices. Even the lower-end ones. Read this article for more info
Make sure that animations are reduced (or even removed) when a user has (prefers-reduced-motion:reduce)
Animations that change visual aspects of an element but don't alter it's layout can still cause performance issues on a large scale. But small effects should be fine (like a hover effect on a button)