CSS: Animations
Illustration by Bruce Heavin

CSS: Animations

with Val Head

Video: Adding interactivity with JavaScript

In this tutorial, we'll be looking at the JavaScript we need to trigger our VS circle animation. If you don't have a lot of experience with JavaScript, don't worry, you'll still be able to follow along with the basic concepts of what we're creating here, and that's the most important part. Just having an idea of how JavaScript in CSS can work together for animations will help you come up with some great ideas of how to you use them in your work. If you'd like to get a little bit more background in JavaScript at any point, you might want to check out a couple of other lynda.com courses. There is a course for JavaScript Essential Training, as well as a course for jQuery Essential Training.
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. 34s
    1. Next steps
      34s

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
please wait ...
Watch the Online Video Course CSS: Animations
1h 56m Intermediate Feb 14, 2013

Viewers: in countries Watching now:

This course presents a short series of CSS animation techniques, such as looping, playing, and pausing, and puts them together in a small project: an animated infographic. Author Val Head also addresses using CSS preprocessors, adding transitions, handling vendor prefixes, and understanding the best uses for CSS animations. Plus, discover how to measure the performance and current level of browser support for CSS animations and how we can expect the technology to change.

Topics include:
  • Understanding delay and fill-mode
  • Dictating an animation's direction
  • Using easing
  • Adding keyframes
  • Looping an animation
  • Chaining multiple animations on one element
  • Setting up the HTML structure
  • Adding interactivity with JavaScript
Subject:
Web
Software:
CSS
Author:
Val Head

Adding interactivity with JavaScript

In this tutorial, we'll be looking at the JavaScript we need to trigger our VS circle animation. If you don't have a lot of experience with JavaScript, don't worry, you'll still be able to follow along with the basic concepts of what we're creating here, and that's the most important part. Just having an idea of how JavaScript in CSS can work together for animations will help you come up with some great ideas of how to you use them in your work. If you'd like to get a little bit more background in JavaScript at any point, you might want to check out a couple of other lynda.com courses. There is a course for JavaScript Essential Training, as well as a course for jQuery Essential Training.

I won't be going through every detail of the JavaScript in this example, just the parts we have to work with for our CSS animations. I've left lots of comments in my final files, though, so if you'd like to explore them further, there's lots of detail there. We'll be writing some JavaScript or specifically jQuery-- which is a JavaScript library--to restart our VS animation. If you remember, we set our animation for this element to play only once. But we actually wanted to play once through every time we make a new coffee selection. So we'll need JavaScript to help us re-trigger that animation. If we head over to Coda and take a look at our JavaScript file, we can see what's going on in there.

The main function we need to be concerned about is this one right here. This function will run anytime an item in our nav is clicked. The bulk of these functions that's already in place deals with making sure the right selection is made and changing things the right color. But we want to add something right at the very end. In our CSS, we created a class called bounce-in, and we assigned the properties of the animation that we wanted our VS circle to have to that class. So what we're going to do to trigger that animation to start is actually assign that bounce-in class to our VS circle when anything in our navigation is clicked.

By, including this little bit of JavaScript to assign the class as part of the function that executes every time the navigation is clicked, means it will be assigned every time that happens. So here at the very bottom of the function, we're going to add one additional line. We'll first address our VS circle which happens to have a class of vs, and we will use a jQuery function called addClass, which as you might imagine adds a class to that particular div or span or element. In this case a span. And we're going to add the class of bounce-in, because that's a class we put all our animation properties in. So now it's just that one line, we've actually triggered an animation to happen with JavaScript.

By assigning that bounce-in class which has all our animation properties to a new element, the animation would immediately execute. So it's one line, but it's very powerful. We can leave this exactly as it is if we only wanted to trigger this animation once. This is exactly what we would need if that's all we were doing. But we've made it a little bit harder on ourselves, and we want to trigger this animation over and over, which means at some point we're also going to have to remove this class, because the animation will only be triggered the first time the class is added. So the question there is how do we know when the animation is done? Because obviously we don't want to remove the class until the animation has finished running.

If we removed it halfway through, the animation would abruptly stop, and that's no good. Lucky for us, browsers automatically broadcast an event when a CSS animation ends. And we can actually listen for these events and do something with them. So in order to do that, I'm going to do this outside of our click function because we only want to set up our listening for this event once. We don't want to keep setting it up every time something is clicked. So we'll move just a little further down on our file outside of that click function. And then to listen for that event, we'll again address our VS circle. We'll be listening for the event of webkitAnimationEnd, and when that event happens, we'll be executing a function.

Within that function, we're going to do one single thing, and that is to remove the class. So in order to remove the class, we'll use a jQuery function of removeClass, which as you can probably imagine, removes the class from an element and the class we'll be removing is bounce-in. So what this little block of code means is that when the webkitAnimationEnd event occurs, we will remove the class of bounce-in from our VS circle, which means it will be ready to be reapplied next time we click on something. These events are very handy, but there's currently one major drawback. Each browser vendor uses a slightly different name for the event.

Kind of like what we see with vendor prefixes, but a little bit worst, because each one has a different name. There absolutely is a method to write this in a way that will catch the events for all browsers, but it's a bit more than I can get into you right here. So I'd recommend checking out this SitePoint article which goes into great detail of how to listen for these events and how to make sure you're capturing the events for all the various browser vendors. So, now with these additions to our JavaScript in place, let's save our file and go back and preview of what we've been working on. So if we refresh our page, now when we select something from our menu, our VS circle bounces in just like we asked it to.

With each click, the bouncing class gets applied and our animation plays. As soon as it hits the end of the animation, that class is automatically removed. And then when we click again, it's added once more and the animation can repeat every time we click. And that's a quick look at how you can use JavaScript to trigger CSS animations. This is a great technique, and there's a lot more you can do with it. So I'd suggest playing around with it and finding ways to work it into your own work.

There are currently no FAQs about CSS: Animations.

 
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.

* Estimated file size

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 ?

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.