Advanced CSS3 Animation Effects

Contributing Editor
This entry is part 3 of 4 in the series CSS3 Animations

CSS3 Animations

We’ve now covered the majority of CSS3 animation properties but there’s another of which few developers are aware…

Pausing and Restarting Animations

The animation-play-state property can pause or resume an animation. It accepts either:

  • running — the default; an animation plays as normal
  • paused — the animation is paused

Typically, you could apply animation-play-state: paused in reaction to user input, e.g. when hovering over or focusing on the animated element:

#myelement:hover, #myelement:focus
{
	animation-play-state: paused;
}

The animation continues from where it stopped once the mouse cursor/focus is moved elsewhere.

Consider my Star Wars Scrolling Text in CSS3. It was just a bit of fun but some users would find it difficult to read the text at the speed it scrolls. Shouting at the projectionist would get you thrown out of the movie theater, but at least we can pause the scrolling in the browser when the user hovers over the text:

#titlecontent:hover
{
	-webkit-animation-play-state: paused;
	animation-play-state: paused;
}

View the Star Wars titles featuring pausing and restarting…

No JavaScript is necessary and no Ewoks were harmed in the creation of this code (although I did bullseye a womp rat and slap a Gungan).

Multiple Animations

Like transitions, you can apply multiple animations to the same element. These are played concurrently — they’re not queued like you would expect in jQuery or other JavaScript libraries. Different animation values are separated by a comma and remember that values will loop if you omit them. For example:

#myelement
{
	animation-name: colorchange, sizechange;
	animation-duration: 5s, 2s;
	animation-timing-function: linear, ease;
	animation-delay: 0s, 5s;
	animation-iteration-count: infinite;
	animation-direction: alternate;
}

This:

  1. runs an alternating ‘colorchange’ animation using the linear timing function for five seconds and starts immediately, and
  2. runs an alternating ‘sizechange’ animation using the ease timing function for two seconds but starts after five seconds.

View a multiple animations demonstration…

You’ll notice that the color change occurs for five seconds before the resizing starts (I’ve also added animation-play-state: paused on hover). Yes, it’s ugly — I’m sure you can devise better examples!

CSS3 Animations

<< CSS3 Animation Property BasicsCSS3 Animation and the JavaScript API >>

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Santhosh

    Good One