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

Working with animation direction

From: CSS: Animations

Video: Working with animation direction

There is one more animation property I'd like to explore here before we say goodbye to our unicycle rider animation, and that's the property of animation direction. Right now we've got our unicycle rider moving across the screen from left to right, but I think his act would be a lot more interesting if he at least changed direction. Animation direction is a property that can help us get a lot more mileage out of our animations without having to make any major changes to our keyframes. Specifically, animation direction lets us manipulate what order our keyframes are executed in. So far our animation has only played forwards, which has worked out pretty well, but we do have other options.

Working with animation direction

There is one more animation property I'd like to explore here before we say goodbye to our unicycle rider animation, and that's the property of animation direction. Right now we've got our unicycle rider moving across the screen from left to right, but I think his act would be a lot more interesting if he at least changed direction. Animation direction is a property that can help us get a lot more mileage out of our animations without having to make any major changes to our keyframes. Specifically, animation direction lets us manipulate what order our keyframes are executed in. So far our animation has only played forwards, which has worked out pretty well, but we do have other options.

Animation direction can be set to normal, reverse, alternate, and alternate reverse. This sounds confusing, but they can really come in handy. An animation direction of normal means that all iterations of the animation will be played as specified. So your keyframes will play from start to end. From the first keyframe you defined to the last one. So, from your from keyframe to your to keyframe or your 0% to your 100%. The animation direction of reverse means that all iterations of your animations will be played in the reverse direction. So your last keyframe will play first and your first keyframe will play last.

So this would play from your to keyframe to your from keyframe or your 100% keyframe to your 0% keyframe. The two alternate versions get a little more complicated, but they're still pretty easy to follow. An animation direction of alternate means that the direction of your animation will alternate with each iteration of the animation. It will start playing in the normal direction so that would be 0% to 100% and the second time around it will play in reverse so that would be 100% to 0. And alternate reverse works the same way, but it starts playing your animation in reverse first and then normal.

Let's go back to our simple ball animation to show you what these do. They're a lot easier to see than to just talk about. So, let's look at our simple ball animation in the browser first to see what happens if we don't specify the animation direction. Normal is the default, and our animation will play forward through our list of keyframes. So if we refresh the page, we'll see our ball goes from left to right. To see what happens when we explicitly set our animation direction to something else, let's go look at the CSS behind this example, and you can find these examples in the exercise folder for this tutorial. So we'll explicitly add our animation direction property, and we'll set it to reverse.

If we save our CSS file now and go back to our browser and preview our ball animation once more, we'll reload the page, and you'll see this time our ball moves from right to left, which is actually going through our keyframes backwards. And just to verify, we'll go back to our CSS, and you will see that was playing our to keyframe to our from keyframe, which is the reverse order that we've actually listed them in, hence the name reverse. So the reverse animation direction plays a little like you were rewinding your animation. So if we go back up and change our animation direction property to alternate, we can see how this one works.

However, alternate can only be used if your animation has an iteration count of more than 1. Because the first time it plays, it will play forwards, and the second time it will play in reverse. So we'll have to make one other change to see this happen, and we'll go up here and change our animation iteration count to 2. That way it will actually have an iteration to alternate the direction on. Let's save our CSS and go back and see how our ball animation looks now. If we refresh our page, our ball animates twice, first in the normal direction and the second time in the reverse direction.

Alternate reverse works much the same as alternate. The only difference is it starts playing the animation in reverse first and then forwards. So if we save this and preview now, you'll see our animation plays in reverse first and then forwards, but it still plays twice, and it still alternates the direction. Now that we've seen all our options, let's go back to our unicycle rider animation and see how we could use animation direction to spice up his act a little bit. So we'll go back to Coda and pull up the CSS for our unicycle rider. I'd like to see him ride in both directions, so I'm going to set our animation direction to alternate.

That way he'll drive both forwards and backwards. Of course, we will also have to adjust our animation iteration count so that way there are two iterations in which to alternate our direction. So we'll go up here and change our animation iteration count to 2, and if we save our file and go back and preview our unicycle rider, we'll refresh the page, we'll see after our 2-second delay, he goes forwards and then backwards, reversing the animation direction just like we indicated. If you have a keen eye, you may have also noticed that our animation timing function was reversed when the direction was reversed. This is a nice built-in touch of CSS animations.

Now that our animation is looking how we'd like it, let's go back to our CSS and add one last step of adding our vendor prefixes. Now we have all our vendor prefixes in place, and we're ready to view this animation on non-WebKit browsers too. With these additional animation properties under your belt, you'll able to create even more interesting effects with your CSS animations without even having to add extra keyframes.

Show transcript

This video is part of

Image for CSS: Animations
CSS: Animations

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