Transitions & Animations
One evolution with CSS3 was the ability to write behaviors for transitions and animations. Front end developers have been asking for the ability to design these interactions within HTML and CSS, without the use of JavaScript or Flash, for years. Now their wish has come true.
With CSS3 transitions you have the potential to alter the appearance and behavior of an element whenever a state change occurs, such as when it is hovered over, focused on, active, or targeted.
Animations within CSS3 allow the appearance and behavior of an element to be altered in multiple keyframes. Transitions provide a change from one state to another, while animations can set multiple points of transition upon different keyframes.
Transitions
As mentioned, for a transition to take place, an element must have a change in state, and different styles must be identified for each state. The easiest way for determining styles for different states is by using the
:hover
, :focus
, :active
, and :target
pseudo-classes.
There are four transition related properties in total, including
transition-property
,transition-duration
, transition-timing-function
, and transition-delay
. Not all of these are required to build a transition, with the first three are the most popular.
In the example below the box will change its
background
color over the course of 1
second in a linear
fashion.1 2 3 4 5 6 7 8 9 10 | .box { background: #2db34a; transition-property: background; transition-duration: 1s; transition-timing-function: linear; } .box:hover { background: #ff7b29; } |
Transition Demo
Transitional Property
The
transition-property
property determines exactly what properties will be altered in conjunction with the other transitional properties. By default, all of the properties within an element’s different states will be altered upon change. However, only the properties identified within the transition-property
value will be affected by any transitions.
In the example above, the
background
property is identified in the transition-property
value. Here the background
property is the only property that will change over the duration of 1
second in a linear
fashion. Any other properties included when changing an element’s state, but not included within the transition-property
value, will not receive the transition behaviors as set by the transition-duration
ortransition-timing-function
properties.
If multiple properties need to be transitioned they may be comma separated within the
transition-property
value. Additionally, the keyword value all
may be used to transition all properties of an element.1 2 3 4 5 6 7 8 9 10 11 12 | .box { background: #2db34a; border-radius: 6px transition-property: background, border-radius; transition-duration: 1s; transition-timing-function: linear; } .box:hover { background: #ff7b29; border-radius: 50%; } |
Transition Property Demo
Transitional Properties
It is important to note, not all properties may be transitioned, only properties that have an identifiable halfway point. Colors, font sizes, and the alike may be transitioned from one value to another as they have recognizable values in-between one another. The
display
property, for example, may not be transitioned as it does not have any midpoint. A handful of the more popular transitional properties include the following.background-color
background-position
border-color
border-width
border-spacing
bottom
clip
color
crop
font-size
font-weight
height
left
letter-spacing
line-height
margin
max-height
max-width
min-height
min-width
opacity
outline-color
outline-offset
outline-width
padding
right
text-indent
text-shadow
top
vertical-align
visibility
width
word-spacing
z-index
Transition Duration
The duration in which a transition takes place is set using the
transition-duration
property. The value of this property can be set using general timing values, including seconds (s
) and milliseconds (ms
). These timing values may also come in fractional measurements, .2s
for example.
When transitioning multiple properties you can set multiple durations, one for each property. As with the
transition-property
property value, multiple durations can be declared using comma separated values. The order of these values when identifying individual properties and durations does matter. For example, the first property identified within the transition-property
property will match up with the first time identified within the transition-duration
property, and so forth.
If multiple properties are being transitioned with only one duration value declared, that one value will be the duration of all the transitioned properties.
1 2 3 4 5 6 7 8 9 10 11 12 | .box { background: #2db34a; border-radius: 6px; transition-property: background, border-radius; transition-duration: .2s, 1s; transition-timing-function: linear; } .box:hover { background: #ff7b29; border-radius: 50%; } |
Transition Duration Demo
Transition Timing
The
transition-timing-function
property is used to set the speed in which a transition will move. Knowing the duration from the transition-duration
property a transition can have multiple speeds within a single duration. A few of the more popular keyword values for the transition-timing-function
property include linear
, ease-in
, ease-out
, and ease-in-out
.
The
linear
keyword value identifies a transition moving in a constant speed from one state to another. The ease-in
value identifies a transition that starts slowly and speeds up throughout the transition, while the ease-out
value identifies a transition that starts quickly and slows down throughout the transition. The ease-in-out
value identifies a transition that starts slowly, speeds up in the middle, then slows down again before ending.
Each timing function has a cubic-bezier curve behind it, which can be specifically set using the
cubic-bezier(x1, y1, x2, y2)
value. Additional values include step-start
, step-stop
, and a uniquely identified steps(number_of_steps, direction)
value.
When transitioning multiple properties, you can identify multiple timing functions. These timing function values, as with other transition property values, may be declared as comma separated values.
1 2 3 4 5 6 7 8 9 10 11 12 | .box { background: #2db34a; border-radius: 6px; transition-property: background, border-radius; transition-duration: .2s, 1s; transition-timing-function: linear, ease-in; } .box:hover { background: #ff7b29; border-radius: 50%; } |
Transition Timing Demo
Transition Delay
On top of declaring the transition property, duration, and timing function, you can also set a delay with the
transition-delay
property. The delay sets a time value, seconds or milliseconds, that determines how long a transition should be stalled before executing. As with all other transition properties, to delay numerous transitions, each delay can be declared as comma separated values.1 2 3 4 5 6 7 8 9 10 11 12 13 | .box { background: #2db34a; border-radius: 6px transition-property: background, border-radius; transition-duration: .2s, 1s; transition-timing-function: linear, ease-in; transition-delay: 0, 1s; } .box:hover { background: #ff7b29; border-radius: 50%; } |
Transition Delay Demo
Shorthand Transitions
Declaring every transition property individually can become quite intensive, especially with vendor prefixes. Fortunately there is a shorthand property,
transition
, capable of supporting all of these different properties and values. Using the transition
value alone, you can set every transition value in the order of transition-property
,transition-duration
, transition-timing-function
, and lastly transition-delay
. Do not use commas with these values unless you are identifying numerous transitions.
To set numerous transitions at once, set each individual group of transition values, then use a comma to separate each additional group of transition values.
1 2 3 4 5 6 7 8 9 10 | .box { background: #2db34a; border-radius: 6px; transition: background .2s linear, border-radius 1s ease-in 1s; } .box:hover { color: #ff7b29; border-radius: 50%; } |
Shorthand Transitions Demo
Animations
Transitions do a great job of building out visual interactions from one state to another, and are perfect for these kinds of single state changes. However, when more control is required, transitions need to have multiple states. In return, this is where animationspick up where transitions leave off.
Animations Keyframes
To set multiple points at which an element should undergo a transition, use the
@keyframes
rule. The @keyframes
rule includes the animation name, any animation breakpoints, and the properties intended to be animated.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @keyframes slide { 0% { left: 0; top: 0; } 50% { left: 244px; top: 100px; } 100% { left: 488px; top: 0; } } |
The animation above is named
slide
, stated directly after the opening @keyframes
rule. The different keyframe breakpoints are set using percentages, starting at 0%
and working to 100%
with an intermediate breakpoint at 50%
. The keywords from
and to
could be used in place of 0%
and 100%
if wished. Additional breakpoints, besides 50%
, may also be stated. The element properties to be animated are listed inside each of the breakpoints, left
and top
in the example above.
It is important to note, as with transitions only individual properties may be animated. Consider how you might move an element from top to bottom for example. Trying to animate from
top: 0;
to bottom: 0;
will not work, because animations can only apply a transition within a single property, not from one property to another. In this case, the element will need to be animated from top: 0;
to top: 100%;
.Animation Name
Once the keyframes for an animation have been declared they need to be assigned to an element. To do so, the
animation-name
property is used with the animation name, identified from the @keyframes
rule, as the property value. The animation-name
declaration is applied to the element in which the animation is to be applied to.1 2 3 4 | .stage:hover .ball { animation-name: slide; } |
Using the
animation-name
property alone isn’t enough though. You also need to declare an animation-duration
property and value so that the browser knows how long an animation should take to complete.Animation Duration, Timing Function, & Delay
Once you have declared the
animation-name
property on an element, animations behave similarly to transitions. They include a duration, timing function, and delay if desired. To start, animations need a duration declared using the animation-duration
property. As with transitions, the duration may be set in seconds or milliseconds.1 2 3 4 5 | .stage:hover .ball { animation-name: slide; animation-duration: 2s; } |
A timing function and delay can be declared using the
animation-timing-function
and animation-delay
properties respectively. The values for these properties mimic and behave just as they do with transitions.1 2 3 4 5 6 7 | .stage:hover .ball { animation-name: slide; animation-duration: 2s; animation-timing-function: ease-in-out; animation-delay: .5s; } |
The animation below should cause the
ball
to bounce once while moving to the left, however only when hovering over the stage
.HTML
1 2 3 4 | <div class="stage"> <figure class="ball"></figure> </div> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | @keyframes slide { 0% { left: 0; top: 0; } 50% { left: 244px; top: 100px; } 100% { left: 488px; top: 0; } } .stage { height: 150px; position: relative; } .ball { height: 50px; position: absolute; width: 50px; } .stage:hover .ball { animation-name: slide; animation-duration: 2s; animation-timing-function: ease-in-out; animation-delay: .5s; } |
Customizing Animations
Animations also provide the ability to further customize an element’s behavior, including the ability to declare the number of times an animation runs, as well as the direction in which an animation completes.
Animation Iteration
By default, animations run their cycle once from beginning to end and then stop. To have an animation repeat itself numerous times the
animation-iteration-count
property may be used. Values for the animation-iteration-count
property include either an integer or the infinite
keyword. Using an integer will repeat the animation as many times as specified, while the infinite
keyword will repeat the animation indefinitely in a never ending fashion.1 2 3 4 5 6 7 8 | .stage:hover .ball { animation-name: slide; animation-duration: 2s; animation-timing-function: ease-in-out; animation-delay: .5s; animation-iteration-count: infinite; } |
Animation Direction
On top of being able to set the number of times an animation repeats, you may also declare the direction an animation completes using the
animation-direction
property. Values for the animation-direction
property include normal
, reverse
,alternate
, and alternate-reverse
.
The
normal
value plays an animation as intended from beginning to end. The reverse
value will play the animation exactly opposite as identified within the @keyframes
rule, thus starting at 100%
and working backwards to 0%
.
The
alternate
value will play an animation forwards then backwards. Within the keyframes that includes running forward from 0%
to 100%
and then backwards from100%
to 0%
. Using the animation-iteration-count
property may limit the number of times an animation runs both forwards and backwards. The count starts at 1
running an animation forwards from 0%
to 100%
, then adds 1
running an animation backwards from100%
to 0%
. Combining for a total of 2
iterations. The alternate
value also inverses any timing functions when playing in reverse. If an animation uses the ease-in
value going from 0%
to 100%
, it then uses the ease-out
value going from 100%
to 0%
.
Lastly, the
alternate-reverse
value combines both the alternate
and reverse
values, running an animation backwards then forwards. The alternate-reverse
value starts at 100%
running to 0%
and then back to 100%
again.1 2 3 4 5 6 7 8 9 | .stage:hover .ball { animation-name: slide; animation-duration: 2s; animation-timing-function: ease-in-out; animation-delay: .5s; animation-iteration-count: infinite; animation-direction: alternate; } |
Animation Play State
The
animation-play-state
property allows an animation to be played or paused using the running
and paused
keyword values respectively. When you play a paused animation, it will resume running from its current state rather than starting from the very beginning again.
In the example below the
animation-play-state
property is set to paused
when making the stage
active by clicking on it. Notice how the animation will temporarily pause until you let up on the mouse.1 2 3 4 5 6 7 8 9 10 11 12 | .stage:hover .ball { animation-name: slide; animation-duration: 2s; animation-timing-function: ease-in-out; animation-delay: .5s; animation-iteration-count: infinite; animation-direction: alternate; } .stage:active .ball { animation-play-state: paused; } |
Animation Fill Mode
The
animation-fill-mode
property identifies how an element should be styled either before, after, or before and after an animation is run. The animation-fill-mode
property accepts four keyword values, including none
, forwards
, backwards
, and both
.
The
none
value will not apply any styles to an element before or after an animation has been run.
The
forwards
value will keep the styles declared within the last specified keyframe. These styles may, however, be affected by the animation-direction
and animation-iteration-count
property values, changing exactly where an animation ends.
The
backwards
value will apply the styles within the first specified keyframe as soon as being identified, before the animation has been run. This does include applying those styles during any time that may be set within an animation delay. The backwards
value may also be affected by the animation-direction
property value.
Lastly, the
both
value will apply the behaviors from both the forwards
and backwards
values.1 2 3 4 5 6 7 8 9 10 11 | .stage:hover .ball { animation-name: slide; animation-duration: 2s; animation-timing-function: ease-in-out; animation-delay: .5s; animation-fill-mode: forwards; } .stage:active .ball { animation-play-state: paused; } |
Shorthand Animations
Fortunately animations, just like transitions, can be written out in a shorthand format. This is accomplished with one
animation
property, rather than multiple declarations. The order of values within the animation
property should be animation-name
,animation-duration
, animation-timing-function
, animation-delay
, animation-iteration-count
, animation-direction
, animation-fill-mode
, and lastlyanimation-play-state
.1 2 3 4 5 6 7 | .stage:hover .ball { animation: slide 2s ease-in-out .5s infinite alternate; } .stage:active .ball { animation-play-state: paused; } |
No comments:
Post a Comment