🖋🖋
Navigate back to the homepage
About

Getting Into CSS3 Animations

January 25th, 2014 · 2 min read

I recently had the opprtunity to dive into some CSS3 animations. I’ve used libraries like animate.css and done animations with javascript, but never did any custom CSS3 work.

The Task

We recently updated our ‘tracking’ iconography at SeatGeek to match our new iPhone app. The lead designer created the heart icon with different states in a PSD and also created the animation below:

Reminder App

What is CSS3 Animation?

In CSS, animation is an effect that lets an element gradually change styles. You create the animations with the @keyframes keyword, which is followed by the name of the animation.

1@keyframes heartAnimation {
2 /* Animation code goes here */
3}

To make the animation cross browser compatible you need to use vendor prefixes:

1@keyframes heartAnimation {
2 /* IE 10+ */
3}
4
5@-webkit-keyframes heartAnimation {
6 /* Safari 4+ */
7}
8
9@-moz-keyframes heartAnimation {
10 /* Fx 5+ */
11}
12
13@-o-keyframes heartAnimation {
14 /* Opera 12+ */
15}

However, for the rest of this post I will exclude the vendor prefixes for the sake of space.

The next step is to add the animation effects and decide when they happen. You can do this with either percentages from 0% to 100% or with the ‘from’ and ‘to’ keyword for simple animations with just a starting and ending state. Below is an example for changing the background color from yellow to blue, and then yellow to green to blue.

1@keyframes colorChange {
2 from {background: yellow;}
3 to {background: blue;}
4}
5
6@keyframes colorChange {
7 0% {background: yellow;}
8 50% {background: green;}
9 100% {background: blue;}
10}

Once the keyframes are created, you can call the animations as CSS properties. For example the code below would run the colorChange animation above 2 times for a 2s duration:

1.color-animation {
2 animation-name: changeColor;
3 animation-iteration-count: 2;
4 animation-duration: 2s;
5}
6
7/* Shorthand */
8.color-animation {
9 animation: changeColor 2 2s;
10}

You can check out all the CSS3 animation properties here.

Planning Out the Animation

After watching the gif several times, I realized it was a slight contraction followed by an expansion to a size slightly larger than the original size, and then back to the original size.

Heart Clicked Animation

Using the CSS3 keyframes and animation syntax above, here is the code I used to make the animation in the gif at the top of this page. It uses the css transform and property to scale the image.

1@keyframes heartAnimation {
2 0% {transform: scale(1,1)}
3 20% {transform: scale(0.9,0.9)}
4 50% {transform: scale(1.15,1.15)}
5 80% {transform: scale(1,1)}
6}
7
8.toggle-animation {
9 animation: heartAnimation 0.7s; // no iteration count is needed as the default is 1 time
10}

For the image, I was using a sprite, so I also needed to change the position of the image to get the red background:

1.toggle-animation {
2 background: url('../images/animation-example-sprite.png') no-repeat -320px 0;
3 animation: heartAnimation 0.7s; // no iteration count is needed as the default is 1 times
4}

Loading Animation

For a loading state, I made the heart white and pulsate in-and-out infinitely. It also scales down and back to the original size, instead of getting slightly larger than the original size before going to the original state like in the heartAnimation code above. Below is the code for the loading state:

1@keyframes loading {
2 0% {transform: scale(1,1) }
3 50% {transform: scale(0.8,0.8) }
4 100% {transform: scale(1,1) }
5}
6
7/* Notice the added 'infinite' to is used to make the animation-iteration-count */
8
9.toggle-loading {
10 background: url('../images/animation-example-sprite.png') no-repeat -160px 0; // make background white
11 animation: loading 1s infinite;
12 -webkit-animation: loading 1s infinite;
13 -moz-animation: loading 1s infinite;
14 -o-animation: loading 1s infinite;
15}

Check Out Demos of the Animations

Here is a site I built to demo the animations:

http://heart-animation.herokuapp.com/

Below is the JS I used to make the animations happen when each icon is clicked. The JS adds and removes the classes that I added the animation properties to.

1$(document).ready(function(){
2
3 $('.animation-1 .image').on('click', function(){
4 $(this).toggleClass('toggle-animation');
5 });
6
7 $('.animation-2 .image').on('click', function(){
8 $(this).toggleClass('toggle-animation-slow');
9 });
10
11 $('.animation-3 .image').on('click', function(){
12 $(this).toggleClass('toggle-loading');
13 });
14
15});

Stay in touch

I send a monthly update with my favorite articles, quotes, and new ideas

You may also like

From Excel to Sublime - My Journey From I-Banking Analyst to Product At SeatGeek

October 13th, 2013 · 4 min read

Sorting a Rails Resource Based on a Calculated Value

October 13th, 2013 · 2 min read
© 2013–2020 Adam Waxman
Link to $https://twitter.com/ajwaxmanLink to $https://unsplash.com/awaxman11Link to $https://github.com/awaxman11Link to $https://instagram.com/awaxman11Link to $https://www.linkedin.com/in/adamjwaxman/Link to $https://dribbble.com/ajwaxman