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

Creating a seamlessly looping animation

From: CSS: Animations

Video: Creating a seamlessly looping animation

In this tutorial we'll be creating an infinite looping animation. CSS is great for this type of animation because it makes it possible to set up an infinite loop with less code than most other methods. We'll be creating a couple of animated clouds that drift across the sky, just like the ones you're looking at here. We'll also look at the animation shorthand syntax and the idea of sharing one animation across multiple elements. So first, let's go into Coda and take a look at the HTML behind this example. You can see our setup is pretty simple. We have a div with the class of sky, and within that we have two divs with the class of cloud.

Creating a seamlessly looping animation

In this tutorial we'll be creating an infinite looping animation. CSS is great for this type of animation because it makes it possible to set up an infinite loop with less code than most other methods. We'll be creating a couple of animated clouds that drift across the sky, just like the ones you're looking at here. We'll also look at the animation shorthand syntax and the idea of sharing one animation across multiple elements. So first, let's go into Coda and take a look at the HTML behind this example. You can see our setup is pretty simple. We have a div with the class of sky, and within that we have two divs with the class of cloud.

Note that each cloud div has two classes, one that is shared between the two of them, and one that's unique to each. We'll use that unique class to create slight variations on the animation for each cloud and the shared class for all the common styles between the two. If we go into our CSS and take a look at our initial styles, you can see we have the shared styles for the clouds within a cloud class and then below that we have two classes called fcloud01 and fcloud02 which have the individual properties for each cloud. The main things that are different between the two clouds is a slightly staggered top position, one is at 100, one is at 240 pixels and a slightly staggered zed or z-index position, depending on which way you like to say that letter.

And basically we're just staggering them one overlapping the other as well. So now that we have our initial styles all set, let's get to adding some animation. First, we'll go to the bottom of our style sheet and add our @keyframes block. I'm going to name this animation drift, since that's pretty much how clouds work. And now we'll get to writing our keyframes. Since in this animation we're basically going to be moving these clouds from somewhere far off to the left to somewhere far off to the right so they disappear on both ends, I'm going to use the keywords of from and to, to define my keyframes because that's exactly what were doing.

So we're going to start out from a position far off to the left, and for that I'm going to use a translation of -250 pixels which will have us far off to the left where we can't see our clouds. So I've set our translation along the X axis to -255 pixels and where I got that number is from knowing that our clouds are 250 pixels wide, so -255 pixels will definitely be so far off to the left we won't be able to see our clouds. Then we'll be animating to a place that's far off to the right.

So I'm just going to copy and paste this since we'll use translate for our to keyframe as well, and this time I'm going to translate to a very high number that I know is all the way off to the right, and that happens to be 1350 pixels, so we're covering a lot of ground here. Now that we have our keyframes set, we can assign this animation to our clouds. We're going to use the same animation for both but adjust the properties in each individual class to make them behave a little differently. So let's start with the fcloud01, and we'll add some animation properties here. First, we're going to assign it the animation of drift since that's the one we just defined.

We'll add an animation duration of 25 seconds because clouds tend not to move very fast, and then we will add an animation timing function of linear, and I'm picking linear here because I want my clouds to move at a constant pace. That may not be realistic of actual clouds, but I prefer the look of it in this case so I'm sticking with it. And we'll add one more bit to our property, which is the iteration-count, and we're going to set this one to infinite, and that's where we're going to get our endlessly looping behavior.

And this animation will repeat over and over infinitely, basically forever. Now that we're starting to get the hang of CSS animation syntax, I think we're ready to start working with the shorthand. We can actually summarize the last four lines we just wrote into one line using the animation syntax shorthand. And I'll show you how. The animation syntax shorthand works like this, we start listing out the name of our animation, the duration we'd like it to take, the timing function that it should use, any delay that might apply, an iteration-count, any direction, and then any fill-mode that may be applied to our animation as well.

If you have trouble remembering the order for this shorthand you can add a comment to your CSS to remind you just like this one. If the shorthand doesn't actually help you work faster, it's really not doing you much of any good. So whatever you have to do to remember the order and what goes in there, definitely do it. So, let's rewrite those last four lines into one line. We'll start with the animation property, and then we will add our animation name which is drift, then our duration of 25 seconds, then our animation timing function of linear, we don't have a delay so we can skip it, and this going to be smart enough to know what we're doing, and then we'll add our animation iteration count of infinite.

And with that, we can delete these top four lines since that last one is doing all the same work. You can see this will really come in handy when you get to adding your vendor prefixes because that's three less lines we need to prefix, and that's a lot of copying and pasting. So now let's go on to our second cloud. We'll copy the animation properties from our first one since we'll just be making a few adjustments and paste it into fcloud02. We'll change a couple of things here for our second cloud. The first thing that we're going to do is increase the duration for our second cloud. So we'll change that to 35 seconds, and this means that our second cloud will be using the same keyframes as the first but will take 10 seconds longer to get through them, so it'll be moving at a slower pace.

In addition to that, I'm going to add a delay to this animation, and according to our shorthand syntax I'm going to add that right here between linear and infinite, and I'm going to add a 10-second delay to this animation. So this way, not only will it be taking longer to do the same keyframes as the first cloud, it will start a little bit later. And last but not least, I'm going to add a fill-mode to the second cloud of backwards and the reason I'm doing that is so during our 10-second delay our second cloud will maintain the position from our first from keyframe, and that way it'll stay off to the left for that 10-second delay before it starts moving.

And one last thing before we preview our animation, let's add all our vendor prefixes so we can look at this in any browser, not just one with WebKit. Now with all our vendor prefixes in place, we can save our file and go back to our browser to preview what we've done. So if we refresh our page, our first cloud starts coming across from the left, going at a very nice slow cloud-like pace, and approximately 10 seconds later our second cloud will show up, going a little bit slower but using the same keyframes as the first. Being able to reuse animations on multiple elements like this is a nice feature of CSS animations.

You could have multiple elements animating with the same keyframes, but differing the properties just a little bit can make things a whole lot more interesting. So it's a good trick to add to your CSS animations.

Show transcript

This video is part of

Image for CSS: Animations
CSS: Animations

29 video lessons · 11736 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 preferencesfrom 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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.