Seasonal Savings: 20% off selected memberships for a limited time. Give now

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Chaining multiple animations on one element

From: CSS: Animations

Video: Chaining multiple animations on one element

We've been creating and applying single animations to create our effects so far. But in practice, it's quite likely that you'll run into a project where you need multiple animations to create the movement you're after. Animating a character or demonstrating a software feature for instance, or even creating a longer playing effect will require more than one animation to complete it. We'll be animating a molecule graphic in isolation for this example, but this chaining technique can be used as part of any kind of linear animated content. We'll have our molecule rolling from the left and then scale up and back into its original position, and in the end it will look a little something like this.

Chaining multiple animations on one element

We've been creating and applying single animations to create our effects so far. But in practice, it's quite likely that you'll run into a project where you need multiple animations to create the movement you're after. Animating a character or demonstrating a software feature for instance, or even creating a longer playing effect will require more than one animation to complete it. We'll be animating a molecule graphic in isolation for this example, but this chaining technique can be used as part of any kind of linear animated content. We'll have our molecule rolling from the left and then scale up and back into its original position, and in the end it will look a little something like this.

Let's go to Coda and take a look at the HTML behind this example. Our HTML is pretty simple. We have a wrapper div and then one div with the class of mol for molecule, and that's the one we'll be using for our animation and the graphic. If we take a look at our starting styles in our CSS, they're also pretty simple. We have a background image and a width and height assigned to our mol class, or our molecule. And we also have some general positioning applied to it before we add our animation. I'm going to use transform to create the roll-in animation first.

I'll be affecting both the translation and the rotation of our molecule. So let's start writing our animation with our keyframes block as usual. I'm going to name this first animation roll-in, since we'll be making it roll in. For our first keyframe at 0%, we will set the translation off to the left so it starts off to the left, and we'll also set the rotation to 0 degrees. So we'll translate on the X axis -200 pixels so that will move our molecule off to the left for the start of our animation, and we'll add an additional transform of a rotation of 0 degrees.

To complete our roll-in animation, we'll add a 100% keyframe. We'll set the same transform properties to different values. So I'm just going to copy this line here, and we're going to set our translateX to 0 pixels, which will have our molecule and up with zero translation on the X axis, which means it will be in its normal position, and we're also going to add a rotation of 360 degrees. Technically, being rotated 360 degrees looks exactly the same as being rotated 0 degrees, but our animation will actually show all those rotation points between 0 and 360 as it plays.

Next, let's define the keyframes for our second animation, and we can do that right below the first in a similar fashion. I'm going to name this animation scale-up, and we'll complete that keyframe block. Our first keyframe will be at 0% of course, and I'm going to start with our scale set to 1, which is the same as 100%. Because for whatever reason, scale works on a scale of zero to 1 as opposed to percentages as you might expect. To give our molecule a bit of spring to it, I'll set the scale to 1.2 at the 25% keyframe.

So you can actually scale things above 100%. You just need to mark it as 1.2 or 3 or whatever amount of scale you want to add to it. Then I'll go back to a scale of 1 at the 60% keyframe, and I'll just copy and paste these to save myself a little typing. Finally, at the 100% keyframe, I'll assign the scale of 1.2 once again. This means our molecule will end up at the end of our animation slightly bigger than the original image. But that's okay, it's just a small scale up, so we shouldn't see any degradation in the image.

In our scale-up animation, I've chosen these percentages and scale values. More or less, it's just what looked best. This is the kind of thing where you might want to set up an animation and then adjust the numbers later to see what really looks best to you. So now with our keyframes all defined, let's assign our animations to our molecule, so we can see how things are shaping up. We'll go back up to our molecule class to assign the animation here. When we assign two animations to one element, we need to use commas to separate the values associated with each animation. So when we're defining the animation-name property, we're actually going to assign two animations.

In the first animation we want to see is the roll-in animation. That may have a comma and the name of the second animation we'd like to see. I know we've started using the animation shorthand in a few of the tutorials. But for this one, I'm going to keep them separate so we can really get a feel for how we're adding two animations. For all the remaining animation properties we'll define for this class, we'll use commas to separate the values for the two animations. So our animation duration would look like this, first, 1 second for our roll-in animation and then 0.75 seconds for our scale-up animation.

Keeping with that trend we'll add our animation-delay. In this case, we're going to have zero second as the animation-delay for our roll-in animation because we want that to play right away. But we don't want the scale-up animation to play until roll-in is done. So we'll set the delay on our scale-up animation to be the same amount of time that our roll-in animation takes. So add a delay of 1 second to our scale-up animation, which means it will wait for that 1 second, and during that 1 second our roll-in animation will take place.

It's a nice little trick for how we chain these animations together on one element. And then we'll add our animation iteration count. And for both of these, we only wanted to play once, because this is kind of a chained together entrance for a molecule. So we're just going to have 1 and 1. And when the values for two animations are the same, we can actually just list it once and then CSS will just continue to use for the next animation. But personally, I like to leave the two comma-separated values because it's a lot easier for me to follow later. But if you prefer just seeing one number there, you can go ahead and do it that way too.

And then as our very last property, we'll add our animation-fill-mode, and I'm going to set this to forward for both. By using forwards for both animations, this means the molecule will keep the 100% keyframe styles at the end of the roll-in animation to be ready for our scale-up. And then once our scale-up animation is done, our molecule will maintain the styles from the 100% keyframe of our scale up animation, and it will end up just how we wanted it. With all these animation properties set, let's save our file and preview what we have so far on our browser.

So if we refresh, we see our roll-in animation taking about one second and then our scale-up animation happening in about 0.75 seconds, and they're each happening one right after each other because of the delay we set up on our scale in animation. Everything is looking pretty good but there's one thing I wanted to add. We didn't define an animation timing function for either of these animations explicitly, which means they're both using the default of ease. But I think I'd like to change that to add just a little bit more style to this animation. So we're going to add one more property to our animation which should be our animation-timing-function.

So we'll add our animation-timing-function here, and for the roll-in, I want it to have a little bit of momentum to kind of start slow and end faster. So it has its momentum going into a scale-up. So I'm going to use ease-in here, which we know will start slow and then accelerate as it goes. And then since our scale-up animation has so many keyframes, the keyframes themselves are kind of indicating a bit of easing on its own. So I'm going to set the animation-timing- function for our scale-up animation to linear, and we'll let the keyframes take care of indicating that easing for us.

We'll go back to our browser and preview our animation again, and it kind of flowed just a bit better. So now that I'm happy with that, once again, our last but not least step is to add in our vendor prefixes so this animation will play in non-WebKit browsers. So it will just take a second to do that. So now with all our vendor prefixes in place, our file is a lot longer, but it's also a much more compatible with non-WebKit browsers for the time being. Now that you have a chained animation to start playing around with, why not go back and add some different easing or maybe change the duration or some other of the animation properties? You might be surprised how much a small change in properties can really change the look and feel of an animation, plus it's really fun to play with.

Show transcript

This video is part of

Image for CSS: Animations
CSS: Animations

29 video lessons · 12714 viewers

Val Head
Author

 
Expand all | Collapse all
  1. 4m 47s
    1. Welcome
      41s
    2. What you should know before watching this course
      58s
    3. Vendor prefixes and browser support
      2m 11s
    4. Using the exercise files
      57s
  2. 28m 16s
    1. Animation basics
      6m 32s
    2. Exploring animation delay and the fill-mode property
      6m 6s
    3. Working with animation direction
      4m 49s
    4. Understanding easing
      8m 47s
    5. Challenge: Adding keyframes to our animation
      49s
    6. Solution: Adding keyframes to our animation
      1m 13s
  3. 47m 37s
    1. Creating a seamlessly looping animation
      6m 27s
    2. Animating an element into place
      5m 43s
    3. Playing and pausing an animation on hover
      6m 20s
    4. Animating 3D transforms
      8m 6s
    5. Preparing a sprite image for animation
      4m 55s
    6. Animating the sprite image with steps
      5m 34s
    7. Chaining multiple animations on one element
      8m 2s
    8. Challenge: Adding a third animation to the chain
      46s
    9. Solution: Adding a third animation to the chain
      1m 44s
  4. 19m 54s
    1. A preview of what we'll be creating
      1m 10s
    2. Setting up the HTML structure
      1m 48s
    3. Adding the header animation
      5m 57s
    4. Animating the data
      6m 8s
    5. Adding interactivity with JavaScript
      4m 51s
  5. 12m 44s
    1. Why and when to use CSS animations
      4m 50s
    2. Organizing animation code for semantics and fallbacks
      4m 51s
    3. Crafting effective animations
      3m 3s
  6. 3m 0s
    1. Helpful online tools for creating and building animations
      3m 0s
  7. 24s
    1. Next steps
      24s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Animations.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.