CSS Transitions & Animations

There are a number of ways of achieving animations with CSS3:
1. Transitions
2. Animations

Similarities
If you’re not looking too closely both transitions and animations look similar. They both allow you to:

  • Specify which CSS properties to listen for changes on
  • Set timing (easing) functions to alter the rate of going from a one property value to another
  • Specify a duration to control how long the animation or transition will take
  • Programmatically listen to animation and transition-specific events that you can then do with as you wish
  • Visualize CSS property changes.

Differences

Once you understand the differences it becomes easier to decide which technique to use as each excels at different things.

Triggering

One of the major differences between animations and transitions can be seen in how you trigger them to start playing.

A transition only plays as a reaction to a CSS property that has changed. A common scenario is one where you use the :hover pseudo class to change the value of a CSS property:

example of hovering to make something larger

To use the example visualized here, if a transition is defined, you would be able to see the circle growing from its normal size to its hover size. Another way of triggering a transition is to use JavaScript to programmatically add or remove CSS classes to simulate a CSS property change. Rounding out our ways of making a property change, you can use JavaScript to set an inline style that changes a property your transition is listening for.

Animations, on the other hand, don’t require any explicit triggering. Once you define the animation, it will start playing automatically. Yep, that’s how animations roll!

Looping

This is pretty simple. Animations can be easily made to loop by setting the animation-iteration-count property. You can specify a fixed number of times you want your animation to repeat:

animation-iteration-count: 5;
If you just want your animation to loop forever, you can do that as well:

animation-iteration-count: infinite;
Transitions, on the other hand, don’t have a property that specifies how many times they can run. When triggered, a transition runs only once. You can make a transition loop by fiddling with the transitionEnd event, but that isn’t particularly straightforward – especially when compared with animations.

Defining Intermediate Points / Keyframes
With an animation, you have the ability to define keyframes which give you more control over your CSS property values beyond just the start and the end:

animations FTW

You can set as many keyframes as you want, and when your animation plays, each keyframe will be hit with the specified property changes reflected. This allows you to create the kinds of involved animations that help HTML5 compete with more established animation technologies like Flash more effectively.

With a transition, you don’t have much control over anything beyond the end result:

transitions are linear

A transition simply goes from an initial state to the final state. You cannot specify any points in-between like you can with an animation, so a transition might not be a good choice if you are trying to create the next Teen Girl Squad sensation or a complex animation.

Specifying Properties Up-Front

The next thing I will describe is how formal animations and transitions are when it comes to defining a transition between CSS property values.

On the formal side, you have transitions. Every CSS property you want recognized by your transition must be explicitly represented.

For example, let’s say you have something like the following:

#mainContent {
background-color: #CC0000;
transition:background-color .5s ease-in;
}
#mainContent:hover {
cursor: pointer;
background-color: #000000;
width:500px;
}
Upon hover, I specify a different value for both background-color as well as width. My transition specifies only background-color though. This means your browser is only listening for changes on the background-color property.

If I wanted my transition to affect both the background-color and width properties, I will need to explicitly add another transition entry for width:

#mainContent {
background-color: #CC0000;
transition:background-color .5s ease-in, width .5s ease-in
}
#mainContent:hover {
cursor: pointer;
background-color: #000000;
width: 500px;
}

Resources

Here’s some examples we went over:

 8 Simple Transitions
Transitions vs Animations
25 CSS3 Animation techniques

Kathleen’s CSS Animation Click Here

CSS Animatable
http://www.cssanimate.com/


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *