navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

CSS: Animations

CSS: Animations

with Val Head

 


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

show more

author
Val Head
subject
Web, Web Graphics, Web Design, Infographics, Web Development
software
CSS 3
level
Intermediate
duration
1h 56m
released
Feb 14, 2013

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:03Hello, I am Val Head, Designer and Consultant. I want to welcome you to CSS: Animations.
00:10I'll show you some of the things you can do with CSS animations and what they're best
00:13at now and into the near future.
00:15We'll start with a solid foundation of the syntax of CSS animations.
00:20We'll build a sample project to see some of it in practice and cover lots of advice for
00:24how you can take advantage of animations in your own work.
00:27We will finish things off with a discussion of Browser Support and helpful tools you can
00:31use to be more productive creating animations.
00:34I'm really excited about the options that CSS animations open up for us, and I hope you are too.
00:39So let's get started.
Collapse this transcript
What you should know before watching this course
00:00I make a few assumptions of what you already know for the material in this course.
00:03So let's take a minute to make sure we're on the same page.
00:07animations are a fun new addition to CSS, but they're probably not the best place to start learning CSS.
00:12You want a basic working knowledge of HTML and CSS coming into this course.
00:17This course will focus on the syntax and use of animation related properties, but we won't
00:21be covering the underlying CSS and HTML in the examples in great detail.
00:25I also assume that you are somewhat familiar with newer CSS properties like transform.
00:30But don't worry if you haven't used it extensively, you don't need to be a CSS transforms expert by any stretch.
00:36If you're new to web design or don't feel you have enough background knowledge of HTML
00:40and CSS, you might want to check out some of the more introductory lynda.com courses,
00:45like CSS Fundamentals, CSS: Page Layouts, or HTML Essential Training.
00:51These courses have been created especially for new web designers and will give you a
00:54great foundation to start this course on, if you don't have it already.
Collapse this transcript
Vendor prefixes and browser support
00:00You'll be seeing a lot of vendor prefixes in the exercises for this course.
00:04So let's take a minute to talk about what they're for and how I'll be using them in this course.
00:09Vendor prefixes are a way for browsers to support features before the spec
00:13for that feature has been finalized.
00:14Sometimes the early implementation of a feature is different from the final version,
00:19or the feature is only partially implemented early on.
00:22Prefixes are a way to let us know that features may not be 100% complete yet.
00:26They're also there to help us work around any slight differences in implementation from
00:29one browser to another while the feature is being finalized.
00:33Vendor prefixes are often hotly debated in the industry.
00:36Love them or hate them, they are a necessary evil of authoring quality CSS that will work across multiple browsers.
00:41At the time of this recording, the most recent versions of Firefox, Opera, and IE support
00:46CSS animations without prefixes.
00:49However, slightly older versions of these browsers, even ones from just a few months ago,
00:53still require prefixes for animation support.
00:56In all of the examples, you'll see me starting out with only the WebKit version of the animation
01:01properties while I'm working on things and getting them where I want them.
01:04By the final version I will always include all of my vendor prefixes.
01:07I'll write out the prefixes in the same order each time starting with the WebKit prefix
01:11version for WebKit browsers, then the moz prefix for Mozilla, the O for Opera, and finally the unprefixed property.
01:19The unprefixed property should always be included last so that their final implementation will
01:23override any experimental implementation.
01:26Thanks to the cascading nature of CSS, the last interpretable property listed will be
01:30the one used by the browser.
01:32All these prefixes certainly look like a lot to type. To help you with that, especially
01:36if you're writing the note by hand, I've included a snippet text file along with the exercise files for this course.
01:42Each animation property you'll use will need to be prefixed. Your keyframe rules also need to be prefixed.
01:47So you'll end up with a long list of keyframe rules like this.
01:50Your unprefixed rules should always come last to keep your code nicely future-proof.
01:54As each browser vendor finalizes its implementation of the animation spec, they will move towards supporting the unprefixed version.
02:00I'll also be using some other newer CSS properties like transforms, and these will also require prefixes.
02:06So get ready to see a whole lot of prefixes, but don't worry, I promise to make things fun.
Collapse this transcript
Using the exercise files
00:00The exercise files for this course are available for both premium and regular members of lynda.com.
00:06You can take just about any of the techniques covered in this course and apply them to your own work right away.
00:11You might also find it helpful to follow along with the specific exercise files.
00:15To access the files, go to the Exercise Files area, and look for the zip file to download,
00:20then extract the folder somewhere easy to find, maybe on your desktop or Documents folder.
00:25The exercise files are organized by chapter and within each chapter there are sub-folders
00:29each named for the tutorial it corresponds to.
00:32Each tutorial's folder contains all the assets you will need to follow along throughout
00:36the tutorial, as well as the final folder with the fully completed version of the tutorial.
00:41You can use whichever you prefer to follow along with.
00:44To keep things easy to follow on screen, I'll be showing all the tutorial examples using the WebKit prefix only.
00:50However, all the exercise files will have the complete list of necessary prefixes currently
00:54required by browser vendors in the final version.
Collapse this transcript
1. Understanding CSS Animations
Animation basics
00:00To get started, let's take a look at the basic ingredients we need to create an animation in CSS.
00:05We'll create a simple animation of a unicycle rider riding across the screen to demonstrate
00:10the main components of a CSS animation.
00:12So we'll be creating an animation that will look a little like this.
00:16If we move over to Coda, we can take a look at the HTML behind this example.
00:20Coda is the text editor I'll be using for all the examples throughout this course.
00:23You can use any text editor you prefer, they will all do pretty much the much the same
00:26thing, but I like to use Coda, because it has a lot of code hinting and other tricks
00:30that help me get things done quickly.
00:31So the HTML behind this example is pretty simple.
00:35We have a div with a class wrapper and then our unicycle rider image within that div.
00:39If we move over to look at our CSS file, we also have some basic styles in place.
00:44We have some positioning applied to our image to make sure it starts where we want it to,
00:47and a few margins and such to keep things away from the very edges of the page.
00:51Really not a whole lot yet. So let's get started to adding some animation.
00:55There are two main parts to any CSS animation.
00:58The first is defining the animation and the second is assigning it to the HTML element
01:02or elements that we'll be using in.
01:05You can do this in any order, but I prefer to define the animation first and then assign it.
01:09To me that just makes more sense, but the order doesn't really matter.
01:13We'll start our CSS animation by defining its keyframes using the @-keyframes rule.
01:19Keyframes are essentially a list describing what should happen over the course of the animation.
01:22We'll define the values for the properties that we'll be animating at various points during the animation.
01:27Any property that we want to have changed over the course of one cycle of the animation
01:31needs to be listed in our keyframes.
01:33So let's take a look at how we write out these keyframes.
01:35We'll go back to our CSS file, and we will start by writing our keyframes block.
01:40And our keyframes block works very similar to any other CSS rule or anything within the
01:46curly brackets is what's considered part of that block.
01:49To complete this @-keyframes block, we also need to give our animation a name.
01:53I'm going to call these animation rides, since we have a man riding in unicycle.
01:57That seems to make sense.
01:58There are few options available to us for how we can define each keyframe within our @-keyframe block.
02:03My personal favorite for animations that are going to move from one place to another, like
02:07the one we're creating here are the keyframes of from and to.
02:11And these work just like they sound.
02:13You define where to animate from and where to animate to.
02:17It's really easy to remember and exactly what we want to do here.
02:20So for this animation, we'll be moving our unicycle rider from its current position so
02:24we'll start out with a translation of zero, which essentially means we are changing nothing
02:30and starting him exactly from where he is right now and then we will animate him to
02:34a position that is 400 pixels further to the right to get him across the screen.
02:39So now our to keyframe will translate our unicycle rider 400 pixels over to the right.
02:46So by translating 400 pixels on the X axis, we'll be moving to the right by 400 pixels.
02:51This is essentially the same as if we had assigned a new left position of 420 pixels
02:56to our unicycle rider, because you'll see he started at the left position of 20,
03:00and then we're moving him 400 pixels to the right.
03:02But generally using translate to move things around, as opposed to animating the left and
03:06top properties is considered to get you better performance.
03:09A lot of community developers have detailed how transforms can take advantage of things
03:13like sub-pixel rendering and hardware acceleration where top and left cannot.
03:18So it's a good idea to stick with translate to move things around.
03:21There's also little chance you would ever use translate to position an element while laying out a page.
03:26So using translate instead of top and left in our animations means there's less chance
03:30we'll accidentally override existing styles.
03:32So now that we have our animation defined, the next step is to assign this animation
03:36to one of our HTML elements.
03:38We're going to assign this animation to our image of our unicycle rider which we know has the class of cycle.
03:44So we'll go up here into this class and add a few more properties.
03:47The first additional property we're going to add is the animation name.
03:51In the animation name we're going to assign it is ride, since that's where we just spent
03:55all the time making keyframes for.
03:57This rule tells our image which set of keyframes to take on.
04:00The second property we want to add is animation duration.
04:04Our keyframes have defined what will change over the course of our animation, but there's
04:07no indication of how long this will take until we define this animation duration property.
04:12I'm going to set it to 2 seconds to have our unicycle rider kind of moving at a--
04:16I guess a relaxed unicycle pace.
04:18If we preview our files right now, you'll see we have some animation happening.
04:22So let's save our CSS and go back over to our browser and see what's happening so far.
04:26So if we refresh our page, we can see our animations starts happening as soon as we refresh.
04:31Just like our keyframes are still there, our unicycle rider image moves across the screen
04:34to the right by 400 pixels, and it does it nice and smoothly.
04:38We've just set the minimum needed to create a CSS animation.
04:41However, there are two other properties I prefer to define explicitly for all my animations,
04:46because it makes my code easier to follow.
04:48So let's go back to our CSS in Coda and just add a couple more properties to our cycle class.
04:53The first is the animation timing function.
04:56This property has a default value if you don't set one explicitly, but I prefer to set this
05:00property for all my animations because it has such an impact on the feel of the animation.
05:04We'll discuss this in more detail in later tutorials, but for now I'm going to set our
05:08unicycle rider's timing function to ease-in.
05:13This will have our unicycle rider starting at slow and then speed up as he gets closer
05:16to the end of his movement.
05:17That will make it look like he's just kind of maybe pedaling a little harder near the
05:21end or something like that.
05:23The second property I like to always include is the animation iteration count.
05:28The animation iteration count determines how many times your animation will play.
05:32As we've just seen, if you don't define this the default is 1.
05:34And that's really how many times I would like this to play right now.
05:37So I'm going to set it to 1, even though that's also the default.
05:40But I like to type that explicitly so it's easier to look at my code and see how many
05:43times my animation is repeating. If I leave it out, I find it harder to remember.
05:47So now that we've added those few properties, let's save our CSS and go back and preview
05:51our animation one last time.
05:52So if we refresh the page, you'll see our unicycle rider starts out slow and then speeds
05:57up at the end, which is reflecting our ease-in animation timing function.
06:01So now that we have the animation looking how we like it, let's go back to Coda and add one last thing.
06:05We're going to need to add in all our vendor prefixes so this animation will play in non-WebKit browsers too.
06:11Okay, so now that we have all our vendor prefixes in place, we'll be able to preview this animation
06:16in a non-WebKit browser that supports animation as well.
06:19Now we have the basics in place for our first CSS animation.
06:23Our unicycle rider moves across the screen smoothly and just how we expected.
06:26Next, we'll look at some additional properties that will let us fine tune this animation for even more control.
Collapse this transcript
Exploring animation delay and the fill-mode property
00:00In the last tutorial, we animated our unicycle rider across the screen.
00:04It was fun but pretty basic.
00:05Lucky for us, there are additional properties that give us a deeper level of control over
00:10our animations and let us give them a little more polish too.
00:12In this tutorial we'll look at properties like animation-delay and animation-fill-mode
00:17and how they can give us more control over how our unicycle rider moves.
00:21Right now our animation starts playing as soon as we load our page.
00:24What if we didn't want the animation to start playing right away?
00:26That's where the animation-delay property comes in handy.
00:30Let's go over to Coda to edit the CSS for this example.
00:33And you can follow along with this example from the exercise files for this tutorial,
00:36or you can work with the files you created last time around.
00:39Either one will have you starting in the same place.
00:41So let's add an animation-delay to our unicycle rider animation.
00:45We'll add one additional property here called animation-delay, and we'll set that delay 2 seconds.
00:50That seems like a nice long enough time that we'll notice.
00:53So if we save our CSS now and go back and preview our animation in our browser, we'll
00:58refresh the page, and we will notice nothing happens for the first 2 seconds and then
01:03our animation executes just like it did before.
01:05Now we've got a nice pause before the action of our animation starts.
01:08You might have also noticed that when our unicycle rider got to the end of the animation,
01:12he jumped back to his original position.
01:14I think I'd rather have him stay at the end off to the right when the animation completes the motion.
01:18To do this, we'll need to take a look at the animation-fill-mode property.
01:22Animation-fill-mode can take on one of four values, none, backwards, forwards, or both.
01:27Let's take a look at a couple of simple ball animations to demonstrate what animation fill mode can do.
01:32None is the value by default if you don't declare the property at all, just like this ball example here.
01:38With the animation-fill-mode of none, it just hops back to its original position once the animation has finished.
01:44This default behavior is pretty awkward looking.
01:46When the animation is over, the target of the animation returns to its intrinsic styles
01:50and the animation styles no longer apply. That's not always what we really want to happen.
01:55If we go to Coda and look at the CSS behind this ball animation, we can make some adjustments
01:59to change how this looks.
02:01If we explicitly set our animation-fill-mode to forwards, our ball will maintain the styles
02:08from the last executed keyframe of the animation after the animation has finished.
02:13In this case, that will mean our to keyframe which is the last one and the current animation we are using.
02:18So if we save our CSS now and go preview this animation once more with just that one property
02:23added, we'll refresh the page, and now we'll see that our ball stays at the end and maintains
02:30those styles from our last keyframe once the animation is complete.
02:33It's looking a lot better.
02:35Now let's take a look at the animation-fill-mode value of backwards.
02:38And to do that, I'm going to go back to Coda and just change the animation our ball is
02:42using to better demonstrate the behavior.
02:44So edit our animation name assignment to take on the other animation we have in our file,
02:49the first one we have listed, called move-left-right.
02:51And you might notice that the keyframes for this animation are defined using percentages,
02:56which is our other option besides from and to.
02:58We'll be working with percentage keyframes later in this course as well.
03:02As you can probably guess, the reason we use percentage keyframes is that we can have more than just two.
03:06While the keywords from and to are really handy, they limit us to two keyframes,
03:11and sometimes we need more than that. So that's where percentages come into use.
03:15With our animation changed, I'm also going to uncomment this animation delay and remove
03:19our fill-mode line for now, so we can see what happens with no fill-mode assigned.
03:23So we'll save the CSS and preview our ball animation now.
03:27When we refresh the page, you will notice there's a jump at the very beginning of our
03:31animation where the ball jumps to the 0% keyframe position and then there is another jump at the end.
03:36So we have just made this ball animation really, really awkward.
03:38Because during the delay it stays in one place then it jumps to the first keyframe of our
03:43animation, and then it jumps back when the animation is done.
03:45So let's assign an animation-fill-mode of backwards.
03:48We'll save this and then preview to see how this changed things.
03:53So with the animation-fill-mode of backwards, our ball will take on the styles from our
03:560% keyframe, the first keyframe in our animation during that animation delay that we set.
04:01So if we go back to our browser and preview, you will see that our animation now starts
04:05in the 0% keyframe position, instead of its original position off to the left.
04:09Of course we are still getting that jump at the end of our animation, and that's where
04:13the animation-fill-mode of both comes in handy.
04:15So if we go back to Coda, we'll change this animation-fill-mode to both.
04:18And then we'll preview our ball animation one last time.
04:22Things are looking a whole lot better.
04:25Our animation stays in the 0% keyframe position during the animation delay at the beginning.
04:29And then once the animation is over, it keeps the styles from our final keyframe as well.
04:34So that's the animation-fill-mode of both.
04:36It takes advantage of both the behaviors of forwards and backwards.
04:39So now that we looked at animation-fill-mode in detail, let's go back to our unicycle rider
04:44animation and see how it can help us out there.
04:47So we'll go back to Coda, and we'll open up the style sheet for our unicycle animation.
04:51It's just how we left it off.
04:52We have our animation delay set of 2 seconds, and now let's add an animation-fill-mode property
04:57to really round out this animation.
04:59Just like the last ball example that we looked at, we have a delay, so we're definitely going
05:02to want to use the fill-mode of backwards.
05:04But we also want our unicycle rider to stay off to the right when it's done, which is
05:08technically the behavior of the animation fill-mode forwards.
05:11So I think we want to use the animation-fill-mode of both here so that way we can take advantage
05:15of the animation-fill-mode of both before the animation starts and after.
05:19So, with that one line changed, let's save our CSS and go back to look at our unicycle animation.
05:25We'll refresh the page, you'll see we still have our delay.
05:28And the animation executes, and when it's done our animation stays right off
05:32to the right where we left it.
05:33So the animation-fill-mode of both and the delay have really added to this animation.
05:37Just to quickly recap how the animation-fill-mode works, the animation-fill-mode of backwards
05:42takes effect before the animation starts, and it applies the styling from the first
05:46executed keyframe during any animation delay.
05:49The animation-fill-mode of forwards applies styling from the last executed keyframe of
05:53your animation after the animation has completed.
05:56And the animation-fill-mode of both gives you both behaviors.
05:59So that is how animation-fill-mode can really round out your animation and create behaviors
06:03that are probably a lot more like what you're looking for.
Collapse this transcript
Working with animation direction
00:00There is one more animation property I'd like to explore here before we say goodbye to our
00:04unicycle rider animation, and that's the property of animation direction.
00:08Right now we've got our unicycle rider moving across the screen from left to right,
00:12but I think his act would be a lot more interesting if he at least changed direction.
00:16Animation direction is a property that can help us get a lot more mileage out of our animations
00:19without having to make any major changes to our keyframes.
00:23Specifically, animation direction lets us manipulate what order our keyframes are executed in.
00:28So far our animation has only played forwards, which has worked out pretty well, but we do have other options.
00:34Animation direction can be set to normal, reverse, alternate, and alternate reverse.
00:38This sounds confusing, but they can really come in handy.
00:41An animation direction of normal means that all iterations of the animation will be played as specified.
00:46So your keyframes will play from start to end.
00:48From the first keyframe you defined to the last one.
00:50So, from your from keyframe to your to keyframe or your 0% to your 100%.
00:55The animation direction of reverse means that all iterations of your animations will
00:58be played in the reverse direction.
01:00So your last keyframe will play first and your first keyframe will play last.
01:04So this would play from your to keyframe to your from keyframe or your 100% keyframe to your 0% keyframe.
01:11The two alternate versions get a little more complicated, but they're still pretty easy to follow.
01:15An animation direction of alternate means that the direction of your animation will
01:18alternate with each iteration of the animation.
01:20It will start playing in the normal direction so that would be 0% to 100% and the second
01:25time around it will play in reverse so that would be 100% to 0.
01:29And alternate reverse works the same way, but it starts playing your animation in reverse first and then normal.
01:34Let's go back to our simple ball animation to show you what these do.
01:37They're a lot easier to see than to just talk about.
01:40So, let's look at our simple ball animation in the browser first to see what happens if
01:44we don't specify the animation direction.
01:46Normal is the default, and our animation will play forward through our list of keyframes.
01:50So if we refresh the page, we'll see our ball goes from left to right.
01:53To see what happens when we explicitly set our animation direction to something else,
01:57let's go look at the CSS behind this example, and you can find these examples in the exercise folder for this tutorial.
02:02So we'll explicitly add our animation direction property, and we'll set it to reverse.
02:08If we save our CSS file now and go back to our browser and preview our ball animation
02:13once more, we'll reload the page, and you'll see this time our ball moves from right
02:18to left, which is actually going through our keyframes backwards.
02:21And just to verify, we'll go back to our CSS, and you will see that was playing our to keyframe
02:26to our from keyframe, which is the reverse order that we've actually listed them in, hence the name reverse.
02:31So the reverse animation direction plays a little like you were rewinding your animation.
02:36So if we go back up and change our animation direction property to alternate, we can see how this one works.
02:42However, alternate can only be used if your animation has an iteration count of more than 1.
02:47Because the first time it plays, it will play forwards, and the second time it will play in reverse.
02:51So we'll have to make one other change to see this happen, and we'll go up here and
02:55change our animation iteration count to 2.
02:57That way it will actually have an iteration to alternate the direction on.
03:01Let's save our CSS and go back and see how our ball animation looks now.
03:05If we refresh our page, our ball animates twice, first in the normal direction and the
03:10second time in the reverse direction.
03:13Alternate reverse works much the same as alternate.
03:16The only difference is it starts playing the animation in reverse first and then forwards.
03:20So if we save this and preview now, you'll see our animation plays in reverse first and
03:24then forwards, but it still plays twice, and it still alternates the direction.
03:29Now that we've seen all our options, let's go back to our unicycle rider animation and
03:33see how we could use animation direction to spice up his act a little bit.
03:37So we'll go back to Coda and pull up the CSS for our unicycle rider.
03:41I'd like to see him ride in both directions, so I'm going to set our animation direction to alternate.
03:49That way he'll drive both forwards and backwards.
03:52Of course, we will also have to adjust our animation iteration count so that way there
03:56are two iterations in which to alternate our direction.
03:59So we'll go up here and change our animation iteration count to 2, and if we save our file
04:04and go back and preview our unicycle rider, we'll refresh the page, we'll see after our
04:102-second delay, he goes forwards and then backwards, reversing the animation direction just like we indicated.
04:17If you have a keen eye, you may have also noticed that our animation timing function
04:21was reversed when the direction was reversed. This is a nice built-in touch of CSS animations.
04:27Now that our animation is looking how we'd like it, let's go back to our CSS and add
04:31one last step of adding our vendor prefixes.
04:34Now we have all our vendor prefixes in place, and we're ready to view this animation on non-WebKit browsers too.
04:41With these additional animation properties under your belt, you'll able to create even
04:44more interesting effects with your CSS animations without even having to add extra keyframes.
Collapse this transcript
Understanding easing
00:00Easing is one of those things that I don't think web designers talk about enough.
00:04It's only one aspect of any given animation, but the easing you choose has the power to
00:08greatly affect the way your animation comes across.
00:10So it's a really important factor.
00:13By definition, easing is the way speed is distributed across the duration of our animation.
00:18In CSS all easing is handled with the animation-timing-function property.
00:22We have three options for defining animation-timing functions, predefined keywords, our own cubic Bezier curves, and steps.
00:30While steps are certainly an option for this property, they have their own distinctive
00:34concept which doesn't involve easing at all.
00:36We'll be talking about them later in their very own tutorial.
00:39There's still plenty of easing to talk about, though.
00:42Let's look at our pre-defined keyword options in more detail first to get a better picture
00:46of what's going on behind the scenes.
00:48Our pre-defined keywords are ease, which is the default, linear, ease-in, ease-out, and ease-in-out.
00:56These all sound a little repetitive, but they kind of build upon each other.
00:59So let's go back and refer to our simple ball animation and see how these different animation-timing-functions
01:04can affect the way the animation looks.
01:06We'll go back to Coda and pull up the CSS for our ball animation.
01:10We're back on our simple move-right animation, which has two keyframes moving from a position
01:15on the left all the way over to positions slightly to the right, and we have our duration,
01:19iteration count, and fill modes already filled in for us.
01:22So let's add an animation-timing-function property.
01:25So let's start with the animation-timing-function of linear.
01:29We'll save the file and then preview our ball animation in our browser and see how that looks.
01:34Our ball moves across the screen rather slow, thanks to our 3-second duration, at a constant
01:39speed the entire time, and that's the way the linear keyword works.
01:42It creates a constant speed of motion across the entire duration of the animation.
01:47This is often perceived as a very mechanical motion, because nothing in real life actually
01:52moves at a constant speed like that.
01:54Let's look at a couple other options to see if we can get something that looks maybe just a bit more organic.
01:59If we go back to Coda and then change our animation-timing-function to ease-in,
02:04let's save that and take a little preview.
02:06We'll refresh our browser window, and you can see that our animation starts out very
02:11slow and speeds up just a bit near the end.
02:14It's still very long, it takes 3 seconds, but you can see a definite speeding up as
02:18we approach the end of the animation, and this is how ease-in works.
02:21It starts out a little slow, and then speeds up towards the end of the animation.
02:25It gives it a feel as if it's kind of taking its time, maybe moving a little bit slow or
02:30perhaps accelerating as it gets near to the end of the animation, and that definitely
02:34feels a little more organic than linear did.
02:37Let's go back to Coda and try ease-out and then preview that.
02:40You'll see we start out a little bit fast and then gradually slow down as we approach
02:44the end of our animation, and that's what ease-out looks like.
02:48And you could see how this is also more organic than linear, similar to how ease-in was,
02:52and this ease-out might be something that's had some force applied to it so that's why it
02:56starts out fast and goes slow, maybe something that was pushed or maybe something that has
03:02weight to it and slows down as it approaches the end of its movement, because it has mass,
03:06and that's slowing it down.
03:08Let's go back to Coda and look at our ease-in-out.
03:10Ease-in-out as you might have guessed combines the look of ease-in and ease-out.
03:16So if we go back to our browser and refresh this to see it again, we start out slow,
03:20go fastest in the middle, and then slow down again at the end.
03:23So again, it's very organic looking, and we have this kind of slowing and starting.
03:27That is something we see more in the real world, because we have things like gravity to deal with.
03:33One last one to check out if we go back to Coda, let's look at our ease keyword, which
03:37if you remember is actually the default animation-timing-function.
03:40So, if you didn't set any animation- timing-function at all, you will get ease.
03:45And if we go back to our browser and preview this, we'll notice it looks quite similar
03:49to the ease-in-out, starts a bit slow, speeds up the most in the middle and then slows down drastically at the end.
03:55I find that the keyword of ease has a more dramatic slowing down at the ends, than the
03:59keyword of ease-in-out, but you can see they look pretty similar.
04:02Personally, I prefer the ease-in-out because of the more balanced motion.
04:05But if you prefer the look of ease, you can use that too.
04:08The next animation-timing-function option on our list is cubic Bezier curves.
04:13These can seem quite complicated at first if you've ever seen them in other people's CSS,
04:16because the math behind them is kind of complicated.
04:19They really can look like a big string of random numbers without much meaning behind
04:23them, but we've already been looking at them.
04:25Each of the keywords options can also be defined as a cubic Bezier curve.
04:29The keywords are a bit like a shortcut to commonly used Bezier curves.
04:33If you want more control than what's offered by the keywords, you can create your own curve.
04:37So let's go to cubic-bezier.com to take a look at what these graphs look like.
04:42When you first get here, it's going to load a random curve for you.
04:44I set up one that happens to have the linear curve first, so let's take a look at that.
04:49If you remember, our animation with the animation-timing- function of linear had a constant speed, and a constant
04:55rate of speed across the entire animation, and this graph here on the left shows one
04:59straight line going up across the whole graph.
05:02And this graph represents the constant speed of that linear animation.
05:06If we were to look at one that was a little bit more complicated or a little bit more
05:09interesting, for example, the ease-in-out, which you can get to by right-clicking and
05:13opening in a new tab on this ease-in-out button.
05:16Now, you can see our graph looks a lot different.
05:19It's got some steep parts and some less steep parts.
05:22And the main thing to remember with this cubic Bezier graphs is the steeper the curve is
05:25the faster the rate of motion, and the less steep it is the slower it is.
05:29So if you follow the time line on this graph, you'll notice it starts at a little bit less
05:34steep, which is that kind of slow motion at the beginning of our ease-in-out and in the
05:38middle of our ease-in-out animation, our animation will be the fastest and have the most speed,
05:43and then as it gets less steep again at the end, we see some slowing down.
05:46Here in this middle area, we have our ease-in-out graph in pink plotted against our regular ease keyword graph here.
05:55And if we hit GO, we can see them kind of race each other.
05:57You will notice they both start and finish at the exact same time.
06:01But the way they get there and the way their speed changes across the animation is different.
06:06And that's why we'd be interested in using different types of easing.
06:10As you choose different graphs, or even maybe adjust the graphs yourself on cubic-bezier.com,
06:14you start to see that small adjustments in the shape of the curve will influence the nuances of our animation.
06:21You should take a few minutes to play around, change these handles and then kind of preview what you've created.
06:27Your graph will always be this top one, and you can see it compared against the regular
06:30ease keyword just to see how things are changing.
06:33CSS allows us to define our own cubic Bezier curves, which make the easing possibilities
06:38available to us nearly endless.
06:40Cease is another tool that let's you define and preview custom cubic Bezier curves.
06:45One additional thing that this tool offers over cubic-bezier.com is the ports of the
06:50pan or easing equations to CSS, and you can see those here in this drop down list of presets.
06:56These offer more refined versions of the basic ease-in, ease-in-out, and ease-out,
07:01easing paradigms that are usually only available in tweening engines.
07:03But with Cease, we can use this in CSS, too, and that's pretty cool.
07:08For both cubic-bezier.com and Cease, once you've got a curve preview that you like,
07:12you can copy and paste the cubic Bezier definition and use it in your own CSS.
07:16Personally, I think that the ease-in-out back looks pretty cool.
07:21That's a pretty fun curve.
07:22We'll preview it here, yeah, that's definitely something I like.
07:25So I can go down here where the code is displayed and grab our cubic Bezier function, copy
07:30and paste that, go back to Coda, and then add this in the animation-timing-function property.
07:36So, now we can go back to our browser and preview our ball animation once again, and
07:41we've got this little bit of an elastic bounce to it, much slower than it was on Cease,
07:45because our duration is quite long.
07:47But you can see how you can use these tools to create these curves and preview them and
07:51then use them in your own work later.
07:53Now that we've taken this more in-depth look at our easing options in CSS, you'll be able
07:58to fine tune your animations with easing choices that will let you get the exact motion
08:01and feel that you're going for.
08:02For example, if you're working on something that's very heavy and may have a lot of mass
08:06and therefore has a lot of gravity acting on it, you might want to work with some variation
08:10of ease-out, and depending on the cubic Bezier variation of ease-out you pick, you can show
08:15how much heavier or how much weight that object has.
08:19If you're working on something that can accelerate, for example, a car that the gas pedal is being
08:23pushed down harder or something going downhill, some variation of ease-in might work, where
08:28it starts out slow and then ends up faster near the end of the motion.
08:33And the variations of ease-in or the cubic Bezier versions of ease-in you'd pick there
08:37can show how much faster or how much steeper the hill is that it's going down or how much more it's accelerating.
08:41So there are a whole lot of options for you to work with and lots you can save with just easing.
Collapse this transcript
Challenge: Adding keyframes to our animation
00:00In this chapter we've been working on our unicycle rider animation, and so far it only
00:04has a starting point and an ending point for the animation.
00:07For a nice follow-up challenge, let's add some additional keyframes to the animation
00:11we've been working on to make this unicycle riding act just a little more complex.
00:16You can use the files that we've already been working with, or you can use the starter files
00:19I provided in the challenge folder.
00:21Remember, keyframes can be defined with percentages, as well as keywords like from and to.
00:26Using percentages fits better with multi-keyframe animations, which is what you are going to want to create here.
00:31So the objectives for this challenge are: to define the animation keyframes in percentages,
00:36to make the unicycle move in more than one direction, and to make the animation repeat at least twice.
00:42This is a nice short challenge, and you should spend about 10 minutes or so adjusting your animation.
00:46Then come back and check out the solution to see what I made.
Collapse this transcript
Solution: Adding keyframes to our animation
00:00So let's take a look what I came up with for this challenge.
00:03I have our unicycle rider going back and forth a little bit at the start, then going off to
00:06the right, and going back and forth a little bit and then completing his routine.
00:10So, let's hop over to the CSS to see how I created this.
00:13I made some adjustment to our rider animation and added a few more keyframes.
00:17I started out with our 0% keyframe of translating nothing, so he starts out in his original position.
00:22Then at 20% I moved our unicycle rider over just a bit to the right, at 35% a little
00:28ways back to the left, and then at 85% I moved him pretty much the entire way over to the right.
00:34And at 100% I moved him ever so slightly over to the left.
00:37So that's how I got that kind of back and forth motion.
00:39And these percentages are numbers I just kind of came up with based on what looked right.
00:43There's no real science or math behind them.
00:45It was just some adjustments here and there to get a good look for the animation.
00:48And in our animation properties, I kept the same animation name, made the duration just
00:52a bit longer to 4 seconds to give him a little bit more of a leisurely pace.
00:56My animation iteration count is 2, so we could do the forwards and backwards alternating motion.
01:01And I also set my animation direction to alternate and my fill mode to forwards, so our unicycle
01:06rider would stay in position at the end of the animation.
01:08So, that's what I came up with.
01:10Hopefully you came up with something fun, too, for your solution.
Collapse this transcript
2. CSS Animation Building Blocks
Creating a seamlessly looping animation
00:00In this tutorial we'll be creating an infinite looping animation.
00:03CSS is great for this type of animation because it makes it possible to set up an infinite
00:07loop with less code than most other methods.
00:10We'll be creating a couple of animated clouds that drift across the sky, just like the ones you're looking at here.
00:15We'll also look at the animation shorthand syntax and the idea of sharing one animation
00:20across multiple elements.
00:21So first, let's go into Coda and take a look at the HTML behind this example.
00:25You can see our setup is pretty simple.
00:27We have a div with the class of sky, and within that we have two divs with the class of cloud.
00:32Note that each cloud div has two classes, one that is shared between the two of them,
00:36and one that's unique to each.
00:37We'll use that unique class to create slight variations on the animation for each cloud
00:42and the shared class for all the common styles between the two.
00:44If we go into our CSS and take a look at our initial styles, you can see we have the shared
00:49styles for the clouds within a cloud class and then below that we have two classes called
00:54fcloud01 and fcloud02 which have the individual properties for each cloud.
00:58The main things that are different between the two clouds is a slightly staggered top
01:02position, one is at 100, one is at 240 pixels and a slightly staggered zed or z-index position,
01:08depending on which way you like to say that letter.
01:10And basically we're just staggering them one overlapping the other as well.
01:14So now that we have our initial styles all set, let's get to adding some animation.
01:17First, we'll go to the bottom of our style sheet and add our @keyframes block.
01:23I'm going to name this animation drift, since that's pretty much how clouds work.
01:28And now we'll get to writing our keyframes.
01:30Since in this animation we're basically going to be moving these clouds from somewhere far
01:34off to the left to somewhere far off to the right so they disappear on both ends,
01:38I'm going to use the keywords of from and to, to define my keyframes because that's exactly what were doing.
01:44So we're going to start out from a position far off to the left, and for that I'm going
01:48to use a translation of -250 pixels which will have us far off to the left where we can't see our clouds.
01:58So I've set our translation along the X axis to -255 pixels and where I got that number
02:03is from knowing that our clouds are 250 pixels wide, so -255 pixels will definitely be
02:10so far off to the left we won't be able to see our clouds.
02:13Then we'll be animating to a place that's far off to the right.
02:16So I'm just going to copy and paste this since we'll use translate for our to keyframe
02:20as well, and this time I'm going to translate to a very high number that I know is all the
02:24way off to the right, and that happens to be 1350 pixels, so we're covering a lot of ground here.
02:31Now that we have our keyframes set, we can assign this animation to our clouds.
02:35We're going to use the same animation for both but adjust the properties in each individual
02:38class to make them behave a little differently.
02:41So let's start with the fcloud01, and we'll add some animation properties here.
02:45First, we're going to assign it the animation of drift since that's the one we just defined.
02:50We'll add an animation duration of 25 seconds because clouds tend not to move very fast,
02:58and then we will add an animation timing function of linear, and I'm picking linear here because
03:05I want my clouds to move at a constant pace. That may not be realistic of actual clouds,
03:10but I prefer the look of it in this case so I'm sticking with it.
03:13And we'll add one more bit to our property, which is the iteration-count, and we're going
03:18to set this one to infinite, and that's where we're going to get our endlessly looping behavior.
03:22And this animation will repeat over and over infinitely, basically forever.
03:26Now that we're starting to get the hang of CSS animation syntax, I think we're ready
03:29to start working with the shorthand.
03:32We can actually summarize the last four lines we just wrote into one line using the animation syntax shorthand.
03:37And I'll show you how.
03:38The animation syntax shorthand works like this, we start listing out the name of our
03:42animation, the duration we'd like it to take, the timing function that it should use, any
03:48delay that might apply, an iteration-count, any direction, and then any fill-mode that
03:52may be applied to our animation as well.
03:55If you have trouble remembering the order for this shorthand you can add a comment to
03:58your CSS to remind you just like this one.
04:00If the shorthand doesn't actually help you work faster, it's really not doing you much of any good.
04:05So whatever you have to do to remember the order and what goes in there, definitely do it.
04:08So, let's rewrite those last four lines into one line.
04:11We'll start with the animation property, and then we will add our animation name which
04:17is drift, then our duration of 25 seconds, then our animation timing function of linear,
04:23we don't have a delay so we can skip it, and this going to be smart enough to know what
04:26we're doing, and then we'll add our animation iteration count of infinite.
04:30And with that, we can delete these top four lines since that last one is doing all the same work.
04:36You can see this will really come in handy when you get to adding your vendor prefixes
04:39because that's three less lines we need to prefix, and that's a lot of copying and pasting.
04:43So now let's go on to our second cloud.
04:46We'll copy the animation properties from our first one since we'll just be making a few
04:49adjustments and paste it into fcloud02.
04:52We'll change a couple of things here for our second cloud.
04:55The first thing that we're going to do is increase the duration for our second cloud.
04:59So we'll change that to 35 seconds, and this means that our second cloud will be using
05:03the same keyframes as the first but will take 10 seconds longer to get through them,
05:07so it'll be moving at a slower pace.
05:10In addition to that, I'm going to add a delay to this animation, and according to our shorthand
05:14syntax I'm going to add that right here between linear and infinite, and I'm going to add
05:17a 10-second delay to this animation.
05:19So this way, not only will it be taking longer to do the same keyframes as the first cloud,
05:24it will start a little bit later.
05:26And last but not least, I'm going to add a fill-mode to the second cloud of backwards
05:31and the reason I'm doing that is so during our 10-second delay our second cloud will
05:36maintain the position from our first from keyframe, and that way it'll stay off to the
05:40left for that 10-second delay before it starts moving.
05:43And one last thing before we preview our animation, let's add all our vendor prefixes
05:47so we can look at this in any browser, not just one with WebKit.
05:51Now with all our vendor prefixes in place, we can save our file and go back to our browser
05:55to preview what we've done.
05:56So if we refresh our page, our first cloud starts coming across from the left, going at
06:02a very nice slow cloud-like pace, and approximately 10 seconds later our second cloud will show up,
06:07going a little bit slower but using the same keyframes as the first.
06:12Being able to reuse animations on multiple elements like this is a nice feature of CSS animations.
06:17You could have multiple elements animating with the same keyframes, but differing the
06:21properties just a little bit can make things a whole lot more interesting.
06:23So it's a good trick to add to your CSS animations.
Collapse this transcript
Animating an element into place
00:00In this tutorial we're going to add a little bit of a personality to a basic social media
00:04nav listing by animating it into place with CSS.
00:07This is a fun way to draw attention to certain elements on the page as it loads, but it's
00:11definitely one of those things that's best in small doses.
00:14We'll also look at animating more than one property in our keyframes.
00:18We'll be creating a slight bounce-in animation for our social media nav.
00:22So when the page loads, it will look a little like this.
00:25Normally, something like this would be in site footer, but for simplicity's sake,
00:29I've left it as the only element on my page.
00:32Let's go into Coda and look at the HTML behind this example.
00:35The HTML behind this nav is pretty typical nav material.
00:39It's a list of links to Flickr, Dribbble, Twitter, and Facebook.
00:42If we switched to our CSS, our starter CSS is a bit more complex and usual due to
00:47the specific background positioning for each logo image in our nav.
00:50Not to worry, though, all the steps adding our animation are still the same despite all these extra starting styles.
00:55I've decided that I'd like to have my social-nav bounce into place.
00:58So, I'll need to create keyframes that first have it placed out of sight above the page
01:03and then I'll have to move it past its normal position at first so it kind of overcompensates
01:08and then move it into its originally intended position.
01:11I'm going to need to do this with three keyframes.
01:13So, let's start writing our @-keyframes block.
01:19I'm going to call this animation bounce-in since that's the effect that we're going for.
01:23I'll just add a bit more space so we can see what we're doing.
01:29I'll be defining my keyframes in percentages since I know I'll need at least three.
01:32So, I'll start it with my 0% keyframe, and in this keyframe I know I want my animation
01:38to start with my navigation above the page essentially out of sight, higher up than we can see in the browser.
01:44So we'll set a translation--a translation along the Y axis of -200 pixels.
01:51So that should move us up and out of view.
01:56And then to create the balance when my animation is almost complete, I want it to move past
02:00its original position just a little bit lower than it should be.
02:03So I'm going to guess that might be around 90% since that's mostly complete, and I will
02:07add a translation here of 8 pixels, so this should be just a little bit lower than it really should be.
02:18And then at 100%, my last keyframe of the animation, I'll add another translate of 0
02:25which will have our navigation end up in exactly the same place as it would have originally
02:29with a translation of nothing, of zero.
02:32We want to have the entire navigation move with this animation so we'll need to assign
02:35the animation to the unordered list that's containing our entire nav.
02:38That's up here in our unordered list with the class of social, so I'll add the animation properties here.
02:45We'll add our animation.
02:48We'll add the animation name of bounce-in since that's the one that we just defined keyframes for.
02:52We'll set our duration to 1 second so it's fairly short, nothing super long in this case.
02:58I'm going to set my animation timing function to linear, and I'm going to have this animation
03:02iteration count set at 1, so it will only play one time.
03:06The direction will be normal and my animation fill mode will be backwards.
03:10And the reason I'm going to add backwards in there is so my navigation will stay out
03:14of sight at the beginning in case there's any delay in the animation being applied.
03:18I'm also setting my timing function to linear to let the spacing of my keyframes implied
03:22the ease-in of our little bounce.
03:24That's easier for me than trying to find just the right ease-in to create the bounce.
03:27It's just one tiny little bounce, so it's pretty easy to do with the keyframe that we've defined.
03:31So now we can save our CSS and go preview what our navigation looks like in the browser.
03:37So if we refresh, our nav starts out of sight, falls in, gets a little bit too far, and then bounces back into place.
03:43So that's pretty much the effect we are going for.
03:45But there's one other thing I'd like to add.
03:47Having an element fade in while it animates into place is a pretty nice touch.
03:51So let's go and add that to our animation as well.
03:54So we'll go back to our CSS and scroll back down to our keyframes, and I'm going to add
03:59an additional property to a couple of our keyframes.
04:01I want to have the nav start out as completely transparent or with its opacity set to 0.
04:07So let's add that in to our 0% keyframe.
04:09Let's make a little space and then add opacity of 0.
04:15So, when our 0% keyframe while our nav placed above the page it will also have opacity of 0.
04:20Obviously, we're going to need to set this to something you can actually see.
04:24So, we'll also add this opacity property to our 90% keyframe.
04:28Make a little space here, and then set the opacity at the 90% keyframe to 1 which is
04:34completely opaque or actually 100% visible.
04:37So, now our navigation will be fading in while it drops into that initial bottom position.
04:41At our 90% keyframe, our opacity is set to 1, which means our navigation is completely
04:46visible, and we don't need to list opacity again in our 100% keyframe because we don't
04:50want it to change any further during the animation.
04:53We want it to stay at 1.
04:55If we don't list the property again in future keyframes to overwrite the last value,
04:59it will continue at the last value we set for the remainder of the animation.
05:02CSS treats animations of multiple properties in the same set of keyframes as nearly independent.
05:08They're part of the same keyframes but they don't all have to change at the exact same time, that's pretty handy.
05:13Let's save our CSS and go back and preview our navigation one more time.
05:18So, we'll refresh the page.
05:20Now, that we've got an effect we like, let's head back to our CSS and add the rest of our
05:24vendor prefixes so this will work in browsers that aren't just WebKit.
05:27Great, so now we have all our vendor prefixes in, and we're all set to preview this in any browser we want.
05:32We've got a nice little effect going on our social-nav now while the page loads, and we've
05:36started developing slightly more complex animations with multiple animated properties within one set of keyframes.
Collapse this transcript
Playing and pausing an animation on hover
00:00In previous tutorials, we created animations that played regardless of user input, as animations tend to do.
00:06CSS is limited in how much they can respond to user input on its own without help from something like JavaScript.
00:11But we can still use that input a little bit here and there.
00:14In this tutorial, we're going to create a button that cycles its background color in
00:17response to the mouse pointer hovering over it and then pauses the animation when the
00:21mouse leaves, just like this button we're looking at right now. When we hover over it, the animation
00:26plays and our background color cycle in an interesting little glowing pattern, and when
00:30we hover off the animation pauses and stops.
00:33You can follow along with the exercise files for this tutorial, or you can skip ahead to
00:37the final to see how everything works out.
00:39Let's go to Coda and take a look at the HTML behind this example.
00:42It's a very simple HTML, once again, and we have a div with the class of button and within
00:47that we have a link with the text of the button called Contact Me.
00:51For right now, our link is going nowhere because this is just an example.
00:54But in a real situation, this link would obviously be going somewhere.
00:57And if we take a look at our starting styles, we have a basic setup, setting a background color
01:01and the shape of our button, as well as the styles to define how our link should look inside our button.
01:06So let's get started by creating our animation.
01:09For this example, we're going to animate between a few background colors to give the button a unique glow effect.
01:14It's definitely something different than we could get with the transition or other hover effects.
01:18So it's unique to animations. First, we'll define our keyframes.
01:26I am going to name this animation glow since that's more or less what we're going to be doing here.
01:30I have picked a few shades of blue and bluish- greens that I'd like to cycle through for my button.
01:35I'll assign each has a background color property at various percentage keyframes.
01:40So I'll start with my 0% keyframe with the background color that I've already picked.
01:44So at our 0% keyframe, we will add the background color
01:49of this shade of blue that I've already picked ahead the time and then add a 20% keyframe.
01:54We'll add another shade of blue, then at 70%, we will change the background color once again.
02:04And then for our last keyframe at 100%, we will set our background color to one final shade.
02:13So I'm writing keyframes like this.
02:15It's nice to have all your colors picked out ahead the time whether you have a specific
02:18palette in Photoshop or something similar.
02:20I picked the percentages based on what looked best to me.
02:23But really, it's just kind of a made-up effect.
02:25So, if certain other percentages or maybe more colors or more keyframes looks better
02:28for you, feel free to add them.
02:30Now that we have created our animation, let's assign this animation to our button.
02:34We'll use the shorthand to assign this animation to our button.
02:37So we'll start by adding the animation property.
02:41We will use the glow animation, since that's the one we just made for our animation name.
02:45For our duration, we'll select 3 seconds, and that's just a duration I've picked as
02:48looking good for cycling through those colors.
02:50We are going to select the animation timing function of ease just to give it a nice in
02:56and out kind of feel to it.
02:57We will repeat this animation an infinite number of times.
03:00So our animation iteration count is infinite, and our animation direction I'm going to set to alternate.
03:07And the reason I am doing this is so that our animation will play forwards through our
03:10keyframes then backwards to our keyframes.
03:12And that will stop it from having a hard cut between the 100% and 0% keyframe, because
03:16it will keep going back and forth so it will be a nice smooth transition through all our colors.
03:21So let's save everything and preview our animation right now.
03:24So we'll go over to our Browser, Preview our animation, and you'll see our button is starting
03:29to cycle colors slowly over the 3 seconds.
03:32Going forwards first then backwards, and sometimes the change is faster or slower, that all depends
03:37on how our keyframes are set up. So everything is looking good.
03:40Let's get on to adding the pause and play functionality to our animation.
03:43So back to our CSS in Coda, we're going to add a couple of things.
03:46First, we'll add one more line to our button class, and what we're going to do set the
03:51animation play state to paused initially.
03:53So we have our animation-play-state, and we'll set that to paused.
04:00Animation-play-state can take two different values.
04:02The animation-play-state can be paused or running and those work just like they sound.
04:07If your play-state is paused, nothing is happening.
04:09It's paused like you hit the Pause button.
04:11And if your animation-play-state is running, it's working like you hit the Play button.
04:15Our animation is now paused by default, and if we previewed our button again now, nothing
04:19would happen at all because it's paused.
04:22Paused animations, however, aren't really anywhere near as much fun as ones that plays.
04:26So let's add a condition for when our animation will actually play.
04:29To do that, we'll add a rule for button:hover, and within that rule, we'll add another animation-play-state property.
04:38So I'll just copy that from up here because it's really long to type, and this time,
04:43I will set it to running.
04:44So what this means is that the animation on our button will be paused initially, but then
04:48when we hover over the button, the animation-play- state will be switched to running and by default,
04:54animations are always in a play-state of running unless we tell it otherwise.
04:57Let's go to our browser and see what our button looks like right now.
05:00When we refresh the page, initially nothing is happening.
05:03Our button is just hanging out like a regular button.
05:06But if we hover over it, suddenly the animation starts playing, and we see that cycling background color.
05:10If we hover off of it, the animation pauses, and you'll notice there is no hard cut.
05:14It just stops right where it was.
05:16And if we hover back over it, the animation picks up again right where it paused and keeps going through its cycle.
05:21There's some potential here to create a very unique hover effect.
05:24In a way, the button appears to behave slightly differently each time you hover over it,
05:27and it is a more complex effect than we could have gotten from using a transition or just a regular CSS hover.
05:33One more time we'll go back to Coda, and in our CSS, we'll add our vendor prefixes since
05:37we're happy with our animation right now.
05:39Now that we have our vendor prefixes in place, this animation will play in non-WebKit browsers too.
05:44CSS animations triggered by hovers are best suited for subtle and non-essential animations
05:49because in older browsers, we wouldn't actually be able to see this effect at all.
05:52For example, if we were looking at this button in a browser that doesn't support animations
05:57at all, we would just see the static button and really nothing would be lost.
06:00So it's important not to include any essential information in animations like this.
06:04For even better control over animations triggered by interaction, you can combine CSS animations
06:09with JavaScript, using JavaScript for the logic and CSS for the animations.
06:14We'll look at that a little bit later in this course, and it's something to look into
06:17if you're really interested in CSS animations.
Collapse this transcript
Animating 3D transforms
00:00We've been working in just 2D so far.
00:02But let's have a look at how we can apply the animation skills we've learn to 3D transforms as well.
00:07If you worked with 3D transforms before, either statically or with transitions, this tutorial
00:12will show you how to use them within animations.
00:14If you haven't worked with 3D transforms before, you may want to review the
00:18CSS Transitions and Transforms course first before watching this tutorial.
00:21I won't be getting into much detail on how the 3D transforms works specifically.
00:26In this tutorial, we'll create an effect on a thumbnail gallery by animating each photo
00:30with a 3D transform into place.
00:32So it will look a little something like this when the page loads.
00:35At the time of this recording, 3D transforms are not widely supported by all browsers.
00:40If we take a look at the caniuse.com Support chart, you can see there's a lot of red squares'
00:44showing no support or dark green squares showing just a little bit of support.
00:49For that reason, I'm creating an effect that isn't critical to my content.
00:53If my page is viewed in a browser that doesn't support 3D transforms, my images will still
00:58display but they'll just be missing that little bit of extra flare.
01:01Nothing critical will be lost, so I don't feel the need to create a fallback alternative
01:05for this particular effect.
01:07We're used to working in the X and Y axis for positioning in CSS.
01:11But with 3D transforms, we have one more axis to consider, the Z or Zed axis depending on
01:16which way you like to say that letter. Our X axis moves from left to right on our page.
01:21Our Y axis is the vertical axis, moving from top to bottom.
01:25Our Zed axis, or Z axis, moves out of the screen towards us, so higher Z values or Zed values
01:32are closer to the viewer.
01:34Lower values on the Zed axis are farther away from the viewer, and the higher values on
01:38the Zed axis are closer to you as a viewer.
01:40We'll be animating the rotation of our photos using the Rotate Transform.
01:45Rotations can happen along any of these three axes.
01:47Every HTML element has an origin point, the point at which all transforms will happen
01:52from and they are center by default. The default origin point is in the middle.
01:56If we rotate an element along its X axis, we will be rotating it in 3D space, and for
02:02the purposes of our animation, we're going to move the origin point on our image to the
02:06top left so it will kind of hinge in from the left side.
02:10Let's go to Coda and have a look at the HTML structure behind this effect.
02:14We have a whole list of images within our wrapper div and each one has the usual things
02:18you'd expect for an image, a source, and width, and height, and it also has one additional
02:22class assigned to each, and that's the class of pic.
02:24If we take a look at our CSS, there are a couple of starting styles that are a bit different
02:29than what we've looked at before.
02:30Specifically in our wrapper div, we have a perspective set 1000 pixels.
02:35For best results when using 3D transforms especially on a group of objects, it's best
02:40to set this perspective at the parent element.
02:42And again, if you're not familiar with the idea of perspective and transforms in 3D,
02:47it will be a good idea to check out the course on CSS Transforms and Transitions to get
02:51a little more insight into why it is we picked this and how we got that number.
02:55Perspective essentially defines the focal length of the imaginary lens you're viewing your object through.
02:59The higher the number, the more flat things will appear, and the lower the number, the
03:03more exaggerated and drastically 3D things will appear.
03:07Additionally in our starting CSS, our images have some positioning styles in place to set them in a grid.
03:12We'll add one more rule to set the transform origin to the top left like we just looked at.
03:18So we'll add our transform-origin, and we will set that to top and left.
03:26Having our images rotate from the left side like we said, we will make it appear more
03:29like a hinge rotation than a rotation around its center.
03:33Everything is in place for our animation.
03:35So let's take quick look at the effect we're going to try to create.
03:39Imagine this is a top-down view of our image and the lower line is our browser window.
03:43We want to have our image start out rotated 90 degrees away from our browser window.
03:48So that way it essentially wouldn't exist at all.
03:51And then as we create our animation, we're going to want to rotate it towards being flat against our browser window.
03:57So it will start out almost like an open door and then our animation will cause that door to shut.
04:02And then once it's shut, our image will be flat up against our screen, if that's really
04:06how web pages worked, and it will appear just like an image normally would.
04:09It takes a little imagination to think in 3D sometimes.
04:12So let's go back to Coda and actually add this animation we just looked at to our CSS.
04:16We'll start out by adding our keyframes blocked just like before, and I'm going to call this
04:22animation Hinge, since we just discussed this is going to look a little bit like a door.
04:26For this animation, since we're kind of creating an open door to a closed door, I'm going to
04:31use the keywords of from and to, to define my keyframes because that really describes what we're doing.
04:36And we're going to start out moving from a rotation along the Y axis of 90 degrees that will be our open door,
04:47so rotation on our Y axis of 90 degrees, and then our closed door is essentially having
04:52our image flat up against the browser window again.
04:55So we'll just copy and paste this line so we can use it again.
04:58And this time we'll set in our to keyframe our rotation of 0 degrees, which is essentially
05:03no rotation and/or exactly where it would be if we hadn't bothered to transform it at all,
05:08and that's where we want things to end up.
05:10With our keyframes defined, let's assign this animation to our image.
05:13We'll assign everything with the class of pic, this animation.
05:19So we'll assign at the animation name of hinge, since that's the animation we just created.
05:23We'll have duration of just 0.5 seconds because we want this effect to happen very quickly
05:27is just an added bit of flare as things load in, not something you need to sit and watch.
05:32I've actually already picked out a cubic-Bezier function to use to define my animation timing function.
05:37I'll just paste that in now. There's no delay, so I will skip that part.
05:42And our animation iteration count will be one. We just want this to happen once.
05:46Our animation direction will be normal. Our animation-fill-mode will be forwards.
05:50And this important so that way our image maintains the styles from our to keyframe once the animation has completed.
05:56So when our animation ends, our image will stay right where it would have been originally
06:00if no effect had happened at all. So it's animating itself into place.
06:04Let's test our animation and see how things look.
06:06So we'll go back over to Chrome, we'll refresh our browser, and you can see all the images
06:12hinged into places if they were kind of closing a door towards the browser window.
06:16For an even more interesting effect, though, we can try and assign some delays to some
06:19of these images because it's a little bit repetitive to see them all swinging at once.
06:23It might be a little interesting to see them stagger a little bit.
06:26So below our class of pic, I'll let a couple of nth-of-type rules to vary the animations
06:30and create a slightly different effect.
06:32So this nth-of-type selector will affect every second image in our list, and we're going
06:38to add an animation delay to it to vary things a little bit.
06:43So we'll add a delay of something small, 0.2 seconds.
06:47The whole duration is only 0.5 seconds.
06:49We want to stagger this, so we want these to start before the first batch ends and then
06:53we'll copy and paste this whole rule, and we will add a second nth-of-type rule for
06:58every third image just to make things ever so slightly more varied, and we'll change
07:03our delay on these ones to 0.3 just to have things staggered even more.
07:07So now that we've added two animation delays, there's one other thing we need to change about our animation.
07:12The very last thing we have listed in our animation shorthand is the animation-fill-mode of forwards.
07:17But now that we're dealing with delays, we need this to happen during the delay of our animation as well.
07:22So we actually now need an animation-fill-mode of both.
07:26And that means that our images will maintain the styles from our first from keyframe during
07:30their 0.2- or 0.3-second delay, and once they are done they'll maintain the styles from the to keyframe.
07:35So now if we save our file and go over to our browser to preview, we'll see we have
07:40a nice staggered hinge effect on our photos, and having them stagger is just a little bit
07:44more interesting than having them all open at the same time.
07:47The result makes for an effect that looks a lot more varied because all the images aren't
07:50animating in at exactly the same time.
07:53So that's the quick look at how we can apply animations to our 3D transforms.
07:57There's obviously a whole lot more you can do with animations in 3D.
07:59So if you're interested in it, it's something you should definitely look into a lot,
08:03and you'll be able to create some really cool effects.
Collapse this transcript
Preparing a sprite image for animation
00:00In this tutorial, we're going to look at the steps involving creating a sprite image
00:04to use in our CSS animations.
00:06Then in the next tutorial, we'll actually use this image and put it in motion with an animation.
00:10This is the image that we'll be creating.
00:12It's a really big image with all the steps to our animation in it.
00:14So you can see it's pretty big, and there's a lot of individual images that we'll put
00:18together to make this one large image.
00:21This CSS animation technique is best for highly illustrated animations and usually ones that
00:25are traditionally drawn and traditionally animated with animation software.
00:29Typically these animations will be created first in something like After Effects or Flash.
00:33We won't cover how to illustrate and animate this in those programs, but we'll be covering
00:37how to bring these animations to life with CSS so they can live natively on the web.
00:42So let's get started with our image. Sprites are often used in web design.
00:46Though they typically look something like this, they're usually a whole bunch of icons
00:50that we want to display in different places on our web page all combined into one
00:54big image, so we can just reference one image to get all our icons.
00:57When we use sprites for animation, the same concept applies.
01:00We collect up each frame of our animation, if you want to think of it that way, and assign
01:04this big image as the background to a div and then move that background to create animation.
01:09So if we hop back to Photoshop, this is the image that we'll be creating for our animation.
01:14The source for this sprite image was an image sequence of an existing traditional animation
01:19exported from the animation program, and what we got out of that was a list of images like this.
01:25It doesn't matter which animation program you use to export these images but all of
01:28the major ones have the ability to export a sequence of images in some way.
01:32The resulting list of images is more important than what you used to export them.
01:36The important thing is to make sure that all your images are the exact same size,
01:39and generally, that will happen by default.
01:41These particular images were prepared by a friend of mine
01:43who does a lot of traditional animation.
01:45Our next step, once we have all our images in the sequence is we'll need to arrange this
01:49into a sprite that we can use in CSS.
01:52There are a number of automated tools for making sprites, but I found that most of them
01:55don't give you enough control over what order your frames will show up in.
01:59We only have a handful of frames in this case, so it's easier to create our sprite image by hand.
02:03We'll go into Photoshop to start this process.
02:05We'll start by pulling the images we have into Photoshop.
02:08We'll use a built-in script to do the hard work of importing each of our images for us.
02:12We'll go to File, then Scripts, and Load Files into Stack.
02:17Then we'll browse to where we have all our images, and mine are in this walker image
02:21folder, select all our images in our sequence and then hit OK.
02:26Then we get to watch Photoshop do all the hard work of importing every single image of our sequence.
02:31In a couple of seconds, you'll see we have a new Photoshop file and every layer in that
02:35file is one of the images from our sequence in order according to their file name.
02:40So that saved us a lot of time.
02:42By default, Photoshop sizes our new image to the same size as the images we imported to create it.
02:47We know that each frame of our animation is 245 pixels by 400.
02:52So our sprite will have to be 245 pixels wide as it is now and 4000 pixels high, because
02:59we have 10 frames, each of them 400 pixels tall.
03:03So we'll change our canvas size to 4000 pixels, and make sure we're anchored at the top.
03:10And if we zoom out, you can see what we've just created, and that's basically the start of our sprite image.
03:16So yes, this is going to be a very big image, and that's why short animations work best for this kind of technique.
03:23Otherwise, your image could get ridiculously huge and probably bigger than you'd ever
03:27want to use on the Internet.
03:29We'll be stacking our images one on top of another, kind of like making a filmstrip.
03:33So I'm going to create some guides to help make sure I get everything to line up accurately.
03:37You can create these guides manually, or if you like shortcuts like me, you can use
03:41a Photoshop plug-in called GuideGuide which you can get from guideguide.me.
03:46It's one that I like to use because it automates the process of making guides, and that's never a fun thing to do.
03:51So back to Photoshop, we'll open our GuideGuide plug-in, and I'll create guides every 400 pixels,
03:55since I know our frames are 400 pixels tall.
03:58And if we zoom out, we can see it has created guides for us every 400 pixels, and it took
04:05at about a second, and it would have taken me a few minutes.
04:08So now we'll arrange each one of our layers in a stack.
04:12But I'll move each layer into place using the guides until we have a stack of all our
04:15frames on top of each other.
04:17Our very last step, once we have all our frames stacked up in order is to export the image
04:21we just made for the web.
04:23Make sure to save your PSD somewhere, too, so you'll have that source file because it didn't
04:27take that long to arrange all these frames, but I don't really want to do it again.
04:30Once you have your file saved, you can export this file for web, and it doesn't need to be transparent.
04:36I'm going to make it a PNG file and then just hit Save, and I'll save this with my files.
04:43Now that we've saved it for web, our image is ready to be animated.
04:47Make a note of the width and height of your image before you close Photoshop.
04:50You'll want to have those handy when we start writing the CSS for the animation in the next tutorial.
Collapse this transcript
Animating the sprite image with steps
00:00In our last tutorial, we took a whole series of images and put them together in one big sprite image.
00:04Now in this tutorial, we're going to use that as the basis of our animation, and we're also
00:09going to look at the one remaining timing function we haven't talked about yet, and that's steps.
00:14We'll be using the sprite image we created in the last tutorial, or you can use the one
00:18in the Exercise File folder from this tutorial to start with.
00:21And we'll use that image as a source for our animation.
00:24At the end of this tutorial, we'll have our sprite image moving like a frame-by-frame animation in our browser.
00:30Steps is one of the options we have for our animation-timing-function, and it's one we
00:33haven't talked about too much yet.
00:35It's the odd one out, because it works a little differently than the rest.
00:38The syntax for steps also looks a bit different.
00:41For the animation-timing-function, you type out steps, and then in brackets you list the
00:45number of steps you'd like to have, which looks a lot different than the previous animation-timing-functions we've looked at.
00:51Steps divides the animation duration into equal parts based on the number of steps you
00:55define so that's why you have to give it that number.
00:58Each of these steps is like a snapshot or a frame of your animation.
01:01It then cycles through each snapshot or frame for the duration of your animation.
01:06So instead of continuous motion, your animation is divided into a series of holds.
01:11Whether you're working from the sprite image you created in the last tutorial or the one
01:14included in the exercise files here, we have our image all ready to go, so let's get it in motion.
01:19We'll go into Coda and look at our basic HTML. It's very simple, as you probably expect.
01:24All we really have in there is one div with the class of sprite, and we'll use this class
01:28to assign a background image and create our animation.
01:31If we take a look at our starting CSS files, we have some properties for our sprite class,
01:35basically width and height, and our background image is set to our sprite image.
01:39However, if we go to our browser and preview this right now, we just have a static image
01:43of the first frame of our animation.
01:45That's a good start, but we need to get it in motion.
01:47Just like the other timing-functions, steps works off a list of keyframes to define your animation.
01:51So let's go back to Coda to define those keyframes.
01:54We'll create a keyframes block.
02:00I'm going to name this animation walker, since that's essentially what he's doing.
02:04We'll start with the 0% keyframe setting our background position to 0, 0.
02:13This is essentially what we have right now, and you'll see our 0% keyframe looks an awful
02:17a lot like the background property in our sprite class above.
02:20Then we'll add a 100% keyframe, setting our background position to 0 and -4000.
02:29If you remember from the last tutorial, or you've taken a look at the image we're working
02:32with, you'll notice that our image is 4000 pixels tall.
02:35So essentially what we're doing with this keyframe animation is moving the image all
02:39the way across its height.
02:40So the background position of your 100% keyframe needs to be equal to the height of your image,
02:45and it also needs to be negative because we want the image to move up.
02:49As our sprite image moves up, frames lower in the image will begin to be shown, and that's
02:53how we'll kind of get this filmstrip style or sprite sheet style animation.
02:57So with our keyframes all set to go, let's apply our animation to our sprite class.
03:02We'll start by adding our animation property, and we'll assign the animation of walker,
03:08which are the keyframes that we just created.
03:10We'll have this animation take 2 seconds, which is a duration I just kind of made up myself for right now.
03:16And then for our timing-function, we'll use steps here.
03:19So we'll write steps, and then in brackets we're going to include the number 10.
03:23The reason that I'm, including 10 here, because I know in my sprite image I combine 10 frames
03:27or 10 separate images to make my sprite image.
03:30So my sprite image has 10 steps to it or 10 frames in it.
03:34If you created a different sprite image that had more frames or less frames, you'd want
03:38to adjust this number, and as well as the background position in our keyframes to reflect
03:42the size and number of frames in the sprite that you are using.
03:45And as one last thing, we will set our animation iteration count to infinite.
03:49Luckily, I know the images I've been provided is actually a repeating walk cycle, so I can
03:55infinitely repeat this animation, and it will loop smoothly.
03:58That's going to depend on the actual images in your sprite.
04:00So some may or may not be created to loop infinitely.
04:03Let's save this and preview our animation now. So we head back to our browser and refresh.
04:09Now we see our animation moving.
04:11And really what we're doing is just animating a background image going up not smoothly but
04:15in 10 steps to show us each of the 10 frames.
04:18Notice how each frame is shown with no transition or easing in between.
04:21It's just a hard cut to the next frame.
04:23The smoothness of the animation is a result of how closely each frame was drawn to the
04:27one before it, and the tendency of our eyes and brain to want to make things appear to be moving smoothly.
04:32So it's a pretty neat effect.
04:34If you're not entirely happy with the way your animation looks, we can make some adjustments
04:37in the CSS to change the overall appearance of your animation.
04:41If we go back to our CSS in Coda, we can do something like change the duration of our
04:46animation and have the walk cycle look entirely differently.
04:49For example, if we set the duration to 1 second instead of 2 and then preview our animation,
04:54you can see he's walking twice as fast.
04:56If we want to exaggerate that even more, we can maybe set the duration to .5 seconds
05:02and go back to our browser and really have him running.
05:05But notice that no matter what, the same number of steps is happening, and we're just making
05:09them happen faster because they're working over a shorter duration.
05:12This technique is perfect for adding little snippets of traditionally drawn animations to a website or app.
05:17It's often used with animated characters like this one, but with just a little imagination
05:21you can use the same technique for reusable transitions between images, maybe a countdown
05:25clock or other things that have a lot of illustrated and repetitive movements.
05:30So it's a great CSS animation technique to be familiar with.
Collapse this transcript
Chaining multiple animations on one element
00:00We've been creating and applying single animations to create our effects so far.
00:05But in practice, it's quite likely that you'll run into a project where you need multiple
00:08animations to create the movement you're after.
00:11Animating a character or demonstrating a software feature for instance, or even creating a longer
00:16playing effect will require more than one animation to complete it.
00:19We'll be animating a molecule graphic in isolation for this example, but this chaining technique
00:25can be used as part of any kind of linear animated content.
00:28We'll have our molecule rolling from the left and then scale up and back into its original
00:33position, and in the end it will look a little something like this.
00:36Let's go to Coda and take a look at the HTML behind this example.
00:41Our HTML is pretty simple.
00:43We have a wrapper div and then one div with the class of mol for molecule, and that's
00:47the one we'll be using for our animation and the graphic.
00:50If we take a look at our starting styles in our CSS, they're also pretty simple.
00:54We have a background image and a width and height assigned to our mol class, or our molecule.
00:59And we also have some general positioning applied to it before we add our animation.
01:04I'm going to use transform to create the roll-in animation first.
01:07I'll be affecting both the translation and the rotation of our molecule.
01:11So let's start writing our animation with our keyframes block as usual.
01:15I'm going to name this first animation roll-in, since we'll be making it roll in.
01:21For our first keyframe at 0%, we will set the translation off to the left so it starts
01:26off to the left, and we'll also set the rotation to 0 degrees.
01:32So we'll translate on the X axis -200 pixels so that will move our molecule off to the
01:36left for the start of our animation, and we'll add an additional transform of a rotation of 0 degrees.
01:42To complete our roll-in animation, we'll add a 100% keyframe.
01:47We'll set the same transform properties to different values.
01:51So I'm just going to copy this line here, and we're going to set our translateX to
01:570 pixels, which will have our molecule and up with zero translation on the X axis, which
02:02means it will be in its normal position, and we're also going to add a rotation of 360 degrees.
02:08Technically, being rotated 360 degrees looks exactly the same as being rotated 0 degrees,
02:15but our animation will actually show all those rotation points between 0 and 360 as it plays.
02:20Next, let's define the keyframes for our second animation, and we can do that right below
02:25the first in a similar fashion.
02:28I'm going to name this animation scale-up, and we'll complete that keyframe block.
02:34Our first keyframe will be at 0% of course, and I'm going to start with our scale set
02:38to 1, which is the same as 100%.
02:42Because for whatever reason, scale works on a scale of zero to 1 as opposed to percentages as you might expect.
02:48To give our molecule a bit of spring to it, I'll set the scale to 1.2 at the 25% keyframe.
02:54So you can actually scale things above 100%.
02:58You just need to mark it as 1.2 or 3 or whatever amount of scale you want to add to it.
03:04Then I'll go back to a scale of 1 at the 60% keyframe, and I'll just copy and paste
03:09these to save myself a little typing.
03:12Finally, at the 100% keyframe, I'll assign the scale of 1.2 once again.
03:18This means our molecule will end up at the end of our animation slightly bigger than the original image.
03:24But that's okay, it's just a small scale up, so we shouldn't see any degradation in the image.
03:28In our scale-up animation, I've chosen these percentages and scale values.
03:32More or less, it's just what looked best.
03:34This is the kind of thing where you might want to set up an animation and then adjust
03:37the numbers later to see what really looks best to you.
03:41So now with our keyframes all defined, let's assign our animations to our molecule,
03:45so we can see how things are shaping up.
03:47We'll go back up to our molecule class to assign the animation here.
03:50When we assign two animations to one element, we need to use commas to separate the values associated with each animation.
03:58So when we're defining the animation-name property, we're actually going to assign two animations.
04:03In the first animation we want to see is the roll-in animation.
04:06That may have a comma and the name of the second animation we'd like to see.
04:11I know we've started using the animation shorthand in a few of the tutorials.
04:16But for this one, I'm going to keep them separate so we can really get a feel for how we're adding two animations.
04:21For all the remaining animation properties we'll define for this class, we'll use commas
04:25to separate the values for the two animations.
04:27So our animation duration would look like this, first, 1 second for our roll-in animation
04:33and then 0.75 seconds for our scale-up animation.
04:38Keeping with that trend we'll add our animation-delay.
04:41In this case, we're going to have zero second as the animation-delay for our roll-in animation
04:46because we want that to play right away.
04:48But we don't want the scale-up animation to play until roll-in is done.
04:53So we'll set the delay on our scale-up animation to be the same amount of time that our roll-in animation takes.
04:59So add a delay of 1 second to our scale-up animation, which means it will wait for that
05:041 second, and during that 1 second our roll-in animation will take place.
05:08It's a nice little trick for how we chain these animations together on one element.
05:12And then we'll add our animation iteration count.
05:17And for both of these, we only wanted to play once, because this is kind of a chained together entrance for a molecule.
05:22So we're just going to have 1 and 1.
05:25And when the values for two animations are the same, we can actually just list it once
05:29and then CSS will just continue to use for the next animation.
05:32But personally, I like to leave the two comma-separated values because it's a lot easier for me to follow later.
05:38But if you prefer just seeing one number there, you can go ahead and do it that way too.
05:43And then as our very last property, we'll add our animation-fill-mode, and I'm going
05:48to set this to forward for both.
05:51By using forwards for both animations, this means the molecule will keep the 100% keyframe
05:55styles at the end of the roll-in animation to be ready for our scale-up.
05:59And then once our scale-up animation is done, our molecule will maintain the styles from
06:04the 100% keyframe of our scale up animation, and it will end up just how we wanted it.
06:10With all these animation properties set, let's save our file and preview what we have so far on our browser.
06:15So if we refresh, we see our roll-in animation taking about one second and then our scale-up
06:20animation happening in about 0.75 seconds, and they're each happening one right after
06:25each other because of the delay we set up on our scale in animation.
06:29Everything is looking pretty good but there's one thing I wanted to add.
06:33We didn't define an animation timing function for either of these animations explicitly,
06:37which means they're both using the default of ease.
06:39But I think I'd like to change that to add just a little bit more style to this animation.
06:44So we're going to add one more property to our animation which should be our animation-timing-function.
06:49So we'll add our animation-timing-function here, and for the roll-in, I want it to have
06:54a little bit of momentum to kind of start slow and end faster.
06:57So it has its momentum going into a scale-up.
07:00So I'm going to use ease-in here, which we know will start slow and then accelerate as it goes.
07:05And then since our scale-up animation has so many keyframes, the keyframes themselves
07:09are kind of indicating a bit of easing on its own.
07:11So I'm going to set the animation-timing- function for our scale-up animation to linear,
07:16and we'll let the keyframes take care of indicating that easing for us.
07:20We'll go back to our browser and preview our animation again, and it kind of flowed just a bit better.
07:26So now that I'm happy with that, once again, our last but not least step is to add in our
07:30vendor prefixes so this animation will play in non-WebKit browsers.
07:33So it will just take a second to do that.
07:37So now with all our vendor prefixes in place, our file is a lot longer, but it's also a
07:42much more compatible with non-WebKit browsers for the time being.
07:45Now that you have a chained animation to start playing around with, why not go back and add
07:49some different easing or maybe change the duration or some other of the animation properties?
07:54You might be surprised how much a small change in properties can really change the look
07:58and feel of an animation, plus it's really fun to play with.
Collapse this transcript
Challenge: Adding a third animation to the chain
00:00For this challenge, I'd like to see you try adding a third animation to the chained animations
00:04we've already been working on.
00:06You can start from the files you had at the end of the last tutorial, or you can find
00:10the same starting files in the Challenge folder for this chapter.
00:13Your third animation can be just about anything.
00:15You can change the opacity, maybe shift the position, anything goes as long as it's
00:19a third animation to extend the effect.
00:22A couple of tips while you work on this challenge, remember to use commas to separate the properties
00:27for multiple animations and make sure you add an animation-delay to your third animation
00:32so that it plays last in the chain of animations.
00:35This challenge should take you around 10 minutes or so, depending on how much time you want
00:38to spend refining or adjusting your final animation.
00:41When you've got one you're happy with, check back to see the Solution file and see what I came up with.
Collapse this transcript
Solution: Adding a third animation to the chain
00:00So here's what I came up with for this challenge.
00:02I'll just refresh the page to show you.
00:05I added a third animation that's just a little bit of a bounce to the end of our chain.
00:09So let's go to Coda and look at the HTML behind my solution.
00:14So I added a third animation named bounce, and I just created a bit of a snappy bounce
00:18effect translating on the Y axis.
00:20To create this bounce effect, I just created a few keyframes translating along the Y axis
00:25to get an up and down bounce.
00:27I started out with the 0% keyframe with no translation at all so we could start kind of from a blank slate.
00:32At 70%, I translated up just a little bit.
00:36And then at 80%, I made a big jump so that 10% jump is pretty big, and it moves pretty fast.
00:41Then at my 100% keyframe, I put everything back to a translation of zero.
00:47If we scroll up to where I added my properties, on our mol class I added the animation-name
00:52of bounce to our comma separated values for all the animation names.
00:55In animation-duration, I had it last 0.5 seconds, and you'll notice it's last on the list with the comma again.
01:02The only kind of trick I guess to this solution is to make sure that the last animation-delay
01:07I added was equal to the duration of the last two animations.
01:11So you'll see I added the delay of 1.75 seconds, which is exactly the combined duration of
01:17the roll-in and scale-up animations which were 1 second and 0.75 seconds, respectively.
01:23So that's my solution, and I hope you came up with something fun for your solution too.
01:28I hope you also remembered to include all your vendor prefixes if you're planning to use
01:31this in any kind of a production environment.
01:34This is another challenge for which there was no real correct answer.
01:37The more you explore how to make things move with CSS and to make your ideas come to life,
01:41the more interesting effects you'll be able to come up with.
Collapse this transcript
3. Project: An Animated Infographic Illustration
A preview of what we'll be creating
00:00In this chapter we're going to be building an animated and interactive infographic
00:04to put some of our CSS animation knowledge into practice.
00:07I've designed a little infographic comparing a few different types of Espresso Drinks and what's in them.
00:12The graphic compares the espresso, milk, and other contents of each drink.
00:16Let's take a look at how the infographic works before we start peeking under the hood.
00:21At the top, we have a little animated header with our coffee cup sliding in and out.
00:24We also have a menu where we can select which drinks to compare.
00:27Each time we click a new drink, our versus circle comes bouncing in to really set off our battle.
00:33The data circles for each drink are also animated in.
00:37You might have noticed that I'm running this example off a local host, and I'm also using some specialty fonts.
00:43These aren't necessary to use the example, I'm just a bit of a font snob, that I wanted
00:46to use my Typekit fonts for this example.
00:48However, if you run the files from our example just from your finder or right of your desktop
00:53as normal, you'll just see slightly different fonts.
00:56So this is what it would look like with the default browser fonts.
00:59Everything still works exactly the same. The type just looks a bit different.
01:03You can work with it either way, and you'll get the same functional results.
01:05In the next two tutorials, we'll take a deeper look into how everything works.
Collapse this transcript
Setting up the HTML structure
00:00Let's start by taking a closer look at the HTML and CSS structure behind our infographic.
00:04We have three main files making up our markup in CSS, the index.html, base.css, and styles.css files.
00:12I'll go through our HTML first.
00:15At the very top of the file, we have a couple of lines here to include Typekit, which I
00:19mentioned in the last tutorial is optional.
00:22Continuing down to our HTML structure, we have one div that's containing our entire infographic.
00:27Then we have a header section holding our title and our coffee cup animation.
00:33Then further down from that, we have our navigation where we select which drink we'd like to look at.
00:38And then below that we have a profile for each drink, listing the espresso, milk, and
00:42other contents of each drink.
00:44And then at the very bottom of our HTML file we include both a reference to jQuery and
00:48the JavaScript file I'll be writing later.
00:51Moving to our CSS files, our base.css file contains the basic typographic structure and
00:57a few zeroing out and re-settings that I'm using just to start off at a good base for our infographic.
01:01We won't be working with this file much as most of the work we'll be doing is in the styles.css.
01:07But feel free to poke around if you're curious of what I have in here.
01:10Moving to our styles.css, this is where the bulk of the CSS we'll actually be working with is.
01:15At the very top, we have an import line importing our base.css and then right below that I have
01:21a little comment with all my colors that I'll be using. I like to keep those handy.
01:24But our styles.css file is sectioned out mostly by the section on the infographic.
01:30For example, we have all our styles for our header right here at the top, then further down the styles
01:35for our nav, then the styles for our drink profiles, and so on.
01:39We'll mostly be dealing with the header section, the profile section, and of course adding an animation section.
01:45So let's take a look at that in the next tutorial.
Collapse this transcript
Adding the header animation
00:00For our first animation of this project, we'll animate the coffee cup in our header.
00:04This animation is a lot like the ones we've done previously in the course, so it's a good one to start with.
00:09Let's take a look at our final version first to get a feel for what we're creating.
00:12I'll just refresh the page and then we'll see after a short delay, our coffee cup slides
00:17into the middle of our frame.
00:19It holds there for a moment and then slides back out in the other direction.
00:23So that's the animation we'll be creating in this tutorial.
00:26If we go to Coda and just look at our HTML briefly, we'll see that these are the two
00:31images we'll be dealing with for this animation.
00:34And that's our coffee cup image and the aperture image that's overlaying our coffee cup.
00:39In my initial CSS positioning, I placed the coffee cup below the aperture image,
00:43so that way it's showing up behind it.
00:45The reason you can see it when it's in the middle is our aperture image is actually transparent in the middle.
00:50So that's the kind of trick to this animation.
00:52The two images layered overtop and the top one is transparent in the middle.
00:55So let's go to our styles.css, and we'll head all the way to the bottom of the file
01:00and start our animation section.
01:02The first thing we're going to start with as usual to create our keyframe is our @keyframes blog.
01:08I'm going to call this animation cup-in because it's the coffee cup moving in.
01:14That seems like it will work.
01:15Now we want our coffee cup to slide in somewhat fast and then stay in the middle and just
01:20kind of hold there for a little bit before it makes its exit.
01:23If you've ever worked with animation software like After Effects or similar, you might
01:27have called that a hold keyframe or a hold.
01:30There aren't really hold keyframes in CSS, but I'll show you how we can get the same idea.
01:35So let's start with the 0% keyframe, and we'll have a transform with a translate of 0,
01:41so that will start our coffee cup off exactly where it is right now.
01:45At about 5%, because we want this animation at least the entrance to happen pretty quickly,
01:50let's add another transform and translate our coffee cup in to about 103 pixels.
01:57Now I've come up with these numbers just by adjusting things and playing it around it first,
02:00so that's why I'm guessing numbers like 103. I've actually checked this out before.
02:05Now we want our coffee cup to kind of hold in that middle at that 103 pixels position.
02:09And in order to do that, what we'll actually do is create another keyframe that's exactly
02:14the same as this 5% keyframe.
02:16So I'm just going to copy this and paste it below and then set this to 80%.
02:22This might look a little bit strange to have a 0%, 5%, and 80% keyframe, where 5% and 80% are exactly the same.
02:29But essentially we're creating an animation with zero change, so our coffee cup will stay
02:34static in one position between 5% and 80%. That's a handy little trick.
02:39At the 90% keyframe, we want our coffee cup to leave the frame off to the right.
02:43So we're going to transform it again just a little further out.
02:47We're going to translate it here to 240 pixels.
02:50That happens to be so far off to the right that our coffee cup image will be covered
02:55up by the opaque portion of our aperture image.
02:58Then one last keyframe we'll be adding is our 100% keyframe, and we're going to repeat
03:02this whole pattern again, because what we want is for our coffee cup to go over to the right
03:07and then stay there just for a little bit out of sight before it comes back in again.
03:11So we will once again do a repetitive keyframe and make our 100% keyframe the same position
03:16as our 90% to create a second sort of hold at the end of our animation.
03:20Now the nice thing is with all these repetitive keyframes is we can actually make our keyframe
03:25notation a lot more compact by doing a couple of things.
03:28Since both our 5% and 80% keyframes are the same, we can actually notate this by a comma separating them.
03:36So adding 5%, 80% with one declaration means we can eliminate this 80% line here and then
03:42we can do the same for the 90% and 100%.
03:45We can add a comma, 100%, and then remove our 100% keyframe line.
03:49Now we've just done this animation with even fewer keyframes, and we don't had to repeat that over and over again.
03:55And one other thing we can remove if we'd like to is when we have a 0% keyframe with a translation of nothing.
04:01That's essentially using the same styles that are already on our coffee cup, and if we delete
04:05this line, CSS will automatically take the existing styles in our coffee cup and make
04:10that into a 0% keyframe, even though we haven't explicitly stated that.
04:14So that's a nice little shortcut as well.
04:17With our keyframes in place, let's go and assign this animation to our coffee cup.
04:21I should probably fix that so we have the proper syntax.
04:24The semicolon should be outside of the brackets of my translate, because the semicolons are
04:29kind of like the end of the sentence when you've created your property, not in the middle of your sentence.
04:34Our coffee cup is in our header and a div with a classic opening, and it has the classic cup.
04:39So we're just going to scroll up a little bit 'til we get there, and here we are.
04:43Opening cup is exactly the image we want to deal with.
04:46So we're going to add our animation properties here, and of course we're going to assign it,
04:50the cup-in animation that we just created.
04:53I'm going to have this animation take 10 seconds, because we want it to be kind of slow, it's going to repeat a lot.
04:59I'll set my animation- timing-function to ease-in.
05:03I'm going to have this repeat infinitely, and I'm also going to add a delay of 5 seconds,
05:09and we'll have our animation-fill-mode set to forwards.
05:13So with that all in place, we can save our file and take a look at what we've created. We'll refresh the page.
05:19And then after a short, approximately 5-second delay, our coffee cup will slide in, it will
05:24hold there for a little bit, that's between our 5% and 80% keyframe, and then it will slide
05:29back out once it hits 80%, and there we go, and it will hold off to the right for just
05:33a short while and then come back and repeat.
05:36Now if you notice, our 5-second delay only occurred the first time the animation played.
05:42After that, the delay we're seeing is really just that fake delay we've created between our 90% and 100% keyframe.
05:48So when infinitely repeating animations, the delay only takes place the first time.
05:53Next, we'll move on to animating the data section of our infographic.
Collapse this transcript
Animating the data
00:00We'll be animating the other two parts of our infographic in this tutorial.
00:04We'll be doing the animations for our VS circle, as well the circles representing our data.
00:09Our VS circle drops in with a hard bounce when we make a new coffee selection, and our
00:13data circles do a sort of less severe drop in when they first appear on the page.
00:18So let's get started writing each of these animations.
00:21We'll head over to our CSS in Coda, back to our little block of animations that we've
00:25set up, and we'll start writing some new keyframes for these two animations.
00:29Let's start with the VS circle first.
00:31So we'll start our keyframe block, and I'm going to call this one bounce-in because that's
00:35very much what it's about to do.
00:37What we want to have happen is we want our VS circle to drop-in from pretty far above.
00:42We want it to fall to some position passed its intended position, then bounce up just
00:47a little bit and then fall down just a bit to its final intended position.
00:52We'll start with our 0% keyframe, and we're going to have this start very high.
00:56So we'll add a transform that translates our VS circle up pretty high, and I'm going to go with -500 pixels.
01:04This is going to be a negative number, because we're moving upwards, and there has to be
01:08a negative value on our Y axis. Then for its first drop, maybe we'll pick 70%.
01:15We'll set another transform and have this fall passed its intended position, so we'll say 30 pixels.
01:22That will be a bit further than it's really going to end up, and then at 80% because these
01:27bounces are getting smaller, we'll add a transform that translates our VS circle just up about
01:3310 pixels, so that will be a small bounce up, and then finally, at 100% we will add transform
01:41that translates our VS circle to 0, which is essentially the exact position it already has styles for.
01:47So those keyframes will create our bounce effect, but I want to add just a little extra touch to this effect.
01:52So I'm going to add just a bit more space here, and I'm going to add a property to
01:57a few of our keyframes, starting with our 0% keyframe.
02:00Though a bounce-in is pretty nice, but personally, I prefer when a bounce-in also is accompanied with a slight change in opacity.
02:07So I'm going to start our VS circle being transparent and set our opacity to 0.
02:10So at the start of its bounce-in when it's making its longest fall, it will actually
02:14be transparent at the beginning.
02:17And then down here at 80%, I'm going to set the opacity to 1, and that will have it be completely opaque.
02:25So in between 0% and 80% keyframe, our VS circle will both be changing its opacity and its position.
02:31So we'll have a nice fade-in while we're doing our bounce-in, and it makes for a nice touch to that effect.
02:37We have one last animation we need to write, and that's the drop-in animation for our circles.
02:42So, we'll start a new keyframes block, and I'm going to call this animation drop-in because that's what they're doing.
02:49Now for this drop-in, we want a movement that's less severe than our bounce-in and really
02:53we just want our circles to start a little higher up than their final position and just have one movement down.
02:59There's no bouncing involved.
03:00So because we only need two positions, we can use the keywords of from and to, to define our keyframes.
03:06And from keyframe I'll set a transform, but I'll translate our data circles up just
03:13a tiny bit, probably about 30 pixels up from their intended position.
03:18And in our to keyframe, I'll do another transform that will translate our circles to 0, so back to their original position.
03:27Just like our bounce-in animation, I'd like to add a little opacity to this one as well.
03:31So in our from keyframe, I'm going to add one more property and set our opacity here to 0.
03:38I'm going to do the same in our to keyframe as well, and we'll set the opacity to 1.
03:44So this way while our data circles are doing that slight drop-in, they'll also do a fade-in
03:47from being completely transparent to being completely opaque.
03:50So now with those sets of keyframes in place, let's go to assigning our animations.
03:56We'll be assigning our VS animation via JavaScript, because we want to have it play each time
04:00a selection is made from our menu.
04:02So instead of adding the animation properties directly to our VS class, we're actually going
04:06to create a new class that we can append or remove with JavaScript later.
04:10We'll do this right up here in our animation section just to keep things all in one place.
04:14So I'm going to create a class called bounce-in, conveniently named the same as my animation.
04:19That's not required, but I like to do it that way.
04:22In this class, we're going to assign our animation properties.
04:24Of course, we're going to assign it the name of bounce-in.
04:28We'll have a duration of 0.5 seconds, because it's going to happen pretty quickly.
04:34Our animation-timing-function will be ease-in, and we're going to have this repeat once,
04:40and we'll add a fill mode of both.
04:43Our data circle's animation will also replay each time a new selection is made.
04:47But since these HTML elements actually get removed and then re-added to the page each
04:51time a selection is made, we won't need JavaScript to trigger they're playing a second time.
04:56So we can add the animation properties directly to our data circles, and these all happen
05:00to be list items within our profile class.
05:04So we'll just scroll up here to find them, here they are, so we can add our animation properties here.
05:10We'll assign the animation of drop-in.
05:13We'll add an animation duration of 0.25 seconds, which is pretty fast, but they're very small
05:18and they don't have far to go.
05:20We'll have an animation-timing-function of ease-out, and I'm going to set an animation delay of 0.5 seconds.
05:26Now you might have noticed that's the exact same amount of time as our VS circle duration was.
05:32So I'm going to have this animation start for all the data circles roughly right after the VS circle has ended.
05:37This timing might not be exact, but it's going to be pretty close.
05:40Then we'll set this to repeat once and have a fill mode of both.
05:45With all that set, we can save our file and head over to our browser to preview what we've done.
05:50Now if we refresh, nothing is going to happen to our VS circle, because we need to add our
05:54JavaScript before the animation will be triggered at all.
05:57But if we scroll down and refresh, we can see that our data circles have their animation in place.
06:02So that's in good progress for this tutorial, and in the next tutorial we'll add some of that JavaScript interactivity.
Collapse this transcript
Adding interactivity with JavaScript
00:00In this tutorial, we'll be looking at the JavaScript we need to trigger our VS circle animation.
00:05If you don't have a lot of experience with JavaScript, don't worry, you'll still be able
00:09to follow along with the basic concepts of what we're creating here, and that's the most important part.
00:14Just having an idea of how JavaScript in CSS can work together for animations will help
00:18you come up with some great ideas of how to you use them in your work.
00:21If you'd like to get a little bit more background in JavaScript at any point, you might want
00:24to check out a couple of other lynda.com courses.
00:26There is a course for JavaScript Essential Training, as well as a course for jQuery Essential Training.
00:32I won't be going through every detail of the JavaScript in this example, just the parts
00:36we have to work with for our CSS animations.
00:39I've left lots of comments in my final files, though, so if you'd like to explore them further,
00:43there's lots of detail there.
00:44We'll be writing some JavaScript or specifically jQuery-- which is a JavaScript library--to restart our VS animation.
00:50If you remember, we set our animation for this element to play only once.
00:54But we actually wanted to play once through every time we make a new coffee selection.
00:57So we'll need JavaScript to help us re-trigger that animation.
01:01If we head over to Coda and take a look at our JavaScript file, we can see what's going on in there.
01:06The main function we need to be concerned about is this one right here.
01:09This function will run anytime an item in our nav is clicked.
01:12The bulk of these functions that's already in place deals with making sure the right
01:16selection is made and changing things the right color.
01:18But we want to add something right at the very end.
01:20In our CSS, we created a class called bounce-in, and we assigned the properties of the animation
01:25that we wanted our VS circle to have to that class.
01:28So what we're going to do to trigger that animation to start is actually assign that
01:32bounce-in class to our VS circle when anything in our navigation is clicked.
01:37By, including this little bit of JavaScript to assign the class as part of the function
01:40that executes every time the navigation is clicked, means it will be assigned every time that happens.
01:45So here at the very bottom of the function, we're going to add one additional line.
01:49We'll first address our VS circle which happens to have a class of vs, and we will use a jQuery
01:54function called addClass, which as you might imagine adds a class to that particular div or span or element.
02:00In this case a span.
02:01And we're going to add the class of bounce-in, because that's a class we put all our animation properties in.
02:06So now it's just that one line, we've actually triggered an animation to happen with JavaScript.
02:10By assigning that bounce-in class which has all our animation properties to a new element,
02:15the animation would immediately execute. So it's one line, but it's very powerful.
02:19We can leave this exactly as it is if we only wanted to trigger this animation once.
02:23This is exactly what we would need if that's all we were doing.
02:26But we've made it a little bit harder on ourselves, and we want to trigger this animation over
02:30and over, which means at some point we're also going to have to remove this class, because
02:34the animation will only be triggered the first time the class is added.
02:38So the question there is how do we know when the animation is done?
02:41Because obviously we don't want to remove the class until the animation has finished running.
02:46If we removed it halfway through, the animation would abruptly stop, and that's no good.
02:50Lucky for us, browsers automatically broadcast an event when a CSS animation ends.
02:55And we can actually listen for these events and do something with them.
02:58So in order to do that, I'm going to do this outside of our click function because we only
03:02want to set up our listening for this event once.
03:04We don't want to keep setting it up every time something is clicked.
03:07So we'll move just a little further down on our file outside of that click function.
03:10And then to listen for that event, we'll again address our VS circle.
03:14We'll be listening for the event of webkitAnimationEnd, and when that event happens, we'll be executing a function.
03:21Within that function, we're going to do one single thing, and that is to remove the class.
03:25So in order to remove the class, we'll use a jQuery function of removeClass, which as
03:29you can probably imagine, removes the class from an element and the class we'll be removing is bounce-in.
03:35So what this little block of code means is that when the webkitAnimationEnd event occurs,
03:39we will remove the class of bounce-in from our VS circle, which means it will be ready
03:43to be reapplied next time we click on something.
03:46These events are very handy, but there's currently one major drawback.
03:49Each browser vendor uses a slightly different name for the event.
03:52Kind of like what we see with vendor prefixes, but a little bit worst, because each one has a different name.
03:58There absolutely is a method to write this in a way that will catch the events for all
04:01browsers, but it's a bit more than I can get into you right here.
04:04So I'd recommend checking out this SitePoint article which goes into great detail of how
04:09to listen for these events and how to make sure you're capturing the events for all the various browser vendors.
04:14So, now with these additions to our JavaScript in place, let's save our file and go back
04:19and preview of what we've been working on.
04:21So if we refresh our page, now when we select something from our menu, our VS circle bounces
04:26in just like we asked it to.
04:28With each click, the bouncing class gets applied and our animation plays.
04:31As soon as it hits the end of the animation, that class is automatically removed.
04:36And then when we click again, it's added once more and the animation can repeat every time we click.
04:40And that's a quick look at how you can use JavaScript to trigger CSS animations.
04:44This is a great technique, and there's a lot more you can do with it.
04:47So I'd suggest playing around with it and finding ways to work it into your own work.
Collapse this transcript
4. Performance, Browser Support, and Fallbacks
Why and when to use CSS animations
00:00With the addition of animation, CSS has joined the list of possible ways we can create motion using web technologies.
00:05In this tutorial, we'll look at how to decide which of our options to use.
00:10Are CSS animations right for your current project?
00:13The answer to that question is rarely as black and white as we'd like it to be.
00:16But I'll give you some factors to consider to help you answer that all important question.
00:21Let's start by looking at the main strength of CSS animation first.
00:24Tests have shown that CSS animations can render faster and be less memory intensive than a JavaScript equivalent.
00:31This happens because the bulk of the work for rendering a CSS animation is handled by
00:35the browser internally, which allows for some increased efficiency.
00:39If you're animating with transforms, as we often have throughout this course, your CSS
00:44animations can also benefit from hardware acceleration.
00:47This means they'll be rendered on a different internal layer by the browser.
00:51If you're interested in reading more about CSS animation performance versus jQuery performance,
00:56I'd recommend checking out this article by Chris Mills on the Opera Dev site.
01:00He goes into a lot more detail and how he tested this and the results that he got.
01:04Personally, the biggest advantage I see for CSS animations is how easily available they are.
01:10There's no plug-ins or libraries to install before you can start working with motion.
01:14You can just jump right in with the technology you're already using, HTML and CSS.
01:18This greatly lowers the barrier to adding compelling bits of motion to your project.
01:22This also makes it easy to add just a little bit of motion without having to go all out crazy.
01:28These are some compelling advantages for sure, but how do you decide if CSS animations are
01:33appropriate for your current project, especially when it comes to client work?
01:38There are two main factors I take into consideration when deciding if CSS animations are right for a particular project.
01:44The first is how critical the content included in or conveyed by the animation is?
01:48The second is which browsers the audiences for this project are most likely to be using?
01:53Let's talk about each of this in just a little more detail.
01:57The best test for deciding if your animation is critical is to ask yourself if the page
02:01or screen in question will still make sense without the animation.
02:05For example, an animation that demonstrates a particular software feature would be critical to someone's understanding.
02:11Whereas, an animation designed to call a little bit of extra attention to a button
02:15on the page may not be so critical.
02:18Next, consider the browsers that will likely be used by your audience.
02:22The easiest way to do this is to look at the browser statistics for the current site and
02:25compare that to a site like caniuse.com that charts browser support for CSS animations.
02:31If there are a large percentage of visitors using older browsers, like IE9, for example,
02:36which we see here has 0 support for animations, there is a good chance that a lot of your
02:40visitors won't see your CSS animations due to lack of browser support.
02:45If your animations are critical, using CSS animations in this case would not be the best idea.
02:50If your animations are critical, and there's a large percentage of less capable or older
02:54browsers in your audience, your audience might be better served by a jQuery based animation
02:59or another JavaScript library with strong older browser support.
03:02However, if your animations don't include critical content, it's a different story.
03:08Using CSS animations in this case will provide a slightly richer experience for those with more capable modern browsers.
03:15Anyone using an older or less capable browser like IE9 won't really be any worse off.
03:21This fills with the concept of progressive enhancement, providing an enhanced experience
03:25for more capable browsers, but making sure that the less capable browsers still have
03:29all the necessary and required content.
03:33If you'd like to learn more about the concept of progressive enhancement, I'd highly recommend
03:36this article on A List Apart, by Aaron Gustafson.
03:40He goes into a lot of detail of the concept behind it and uses a lot of really great metaphors and illustrations.
03:46One last consideration when deciding whether or not to use CSS animations in a project is mobile browsers.
03:52The range of mobile browsers out there today makes for a very interesting landscape.
03:57Many of these mobile browsers, mobile safari for example, handles CSS animations nearly perfectly.
04:02But there are some very interesting quirks out there in mobile browsers, which maybe
04:06only partially supports CSS animations, and that happens a lot especially in the Android Market.
04:12It's impossible to go over every possible device and browser combination here, but if
04:16you happened to be targeting a specific device or browser version with your project,
04:20it's best to start testing early and often to be sure it has the support you need.
04:25If I could sum up all my advice on this topic in just one thing, I would say to always consider
04:29your audience first and go with your gut on what makes the most sense for them.
04:34There are plenty of opportunities to use CSS animations to enhance the interactions of
04:38your site or web app and the opportunities are only going to increase in a not so distant future.
04:43As long as you're sure to consider the goals of your audience, you'll be off to
04:46a great start adding CSS animations to your work.
Collapse this transcript
Organizing animation code for semantics and fallbacks
00:00Throughout this course we've been dealing with animations mostly in isolation.
00:03In our real world projects, however, our animations are going to have to mingle with all the other code and assets in our projects.
00:10First, let's take a second to talk about vendor prefixes.
00:12Now I won't remind you yet again why you should use them, but let's look at a couple of ways
00:16that make dealing with them a whole lot easier.
00:18All of these vendor prefixes do add an awful lot of length to your code, especially when it comes to animations.
00:25Luckily, you don't have to type it out all yourself.
00:27If you're already using a CSS preprocessor like Sass or LESS, these both have automated
00:32ways to take care of adding the prefixes for you.
00:35And actually, if you don't use Sass or LESS already, that feature alone is worth checking them out for.
00:41If you prefer not to work with preprocessors, there are still options to save yourself some headaches.
00:46You can set up some saved code snippets in your text editor, or you could download a
00:49version of something like Prefixer, which you can actually install and use with your
00:54text editor if it's one that it has an extension for.
00:58There are also online tools like CSS3Please and the CSS3Generator.
01:04Both of these tools will let you preview your CSS in the browser and then copy and paste
01:08all the prefixes used into your own style sheet.
01:11We'll look at these in a little more detail later to explain exactly how to use them in your work.
01:15Any of these tools I mentioned will help keep your prefix code more manageable.
01:20At the time of this recording, many of the latest modern browser versions are starting
01:24to support animation properties without prefixes.
01:27For example, IE 10, Firefox 16 and higher, as well as Opera 12 and higher support unprefixed animation properties.
01:35As this trend continues, you may be able to drop vendor prefixes for animation sometime
01:40in the near future for certain projects. But we're not quite at that point yet.
01:44The question of where to put your CSS animation code comes up a lot.
01:47Your current workflow and the project in hand will have a lot to do with this, but here's what I would suggest.
01:53Putting all of your animation code in a separate CSS file, maybe even one called animation.css
01:58is an extremely easy way to find your animations any time you might need to edit them.
02:04It also keeps all the multiple browser prefixes out of sight and out of the way when you don't need them.
02:09An animation CSS file might look a little something like this.
02:14You can see that the animation properties have been assigned to specific HTML classes
02:17here at the top of the file.
02:19And these classes can be used to apply the animation to various elements.
02:23Each set of properties includes all the necessary vendor prefixes too.
02:27The second part of the CSS animation file contains all the @keyframe rules.
02:32Splitting the animation properties and the @keyframe rules like this isn't required,
02:36but it makes for a nicely organized and easy file to navigate.
02:40Defining your animation properties and individual classes like this file shows at the top,
02:44can also be very useful to get more mileage out of your animations.
02:48You can apply these classes dynamically with JavaScript for even greater flexibility.
02:53Last but not least, what happens when a browser that lacks CSS animation support encounters
02:58your CSS animation code? It simply ignores it.
03:01So in the case of older browsers which don't support animations at all, your animations will simply be ignored.
03:06This is no big deal if your animations are part of the behavior layer and don't hold
03:10any imperative content that isn't otherwise available.
03:13However, if it's imperative that your animations are seen, even in these older or less capable
03:19browsers, you'll need to create a fall back method.
03:22Tools like Modernizr allow you to detect support for specific CSS features and provide alternate
03:27content or options if they're not supported.
03:30Briefly, to use Modernizr you go to the download section and select the features you would like to detect for.
03:37In our case, this would be CSS animations, and you can select any other properties you may be interested in.
03:43Then you scroll down and hit the Generate button to download your own custom version
03:48of Modernizr that's only going to work for the properties you indicated.
03:51Then you can download your Custom Build.
03:54Once you have your Custom Build of Modernizr downloaded, you would include it in the head
03:58of your HTML document, something like this.
04:01And then you would just create a simple check for Modernizr.cssanimations.
04:06And then within that, you can select what to do if CSS animations are supported or not.
04:11For example, if they're not supported, you may include alternate CSS or perhaps a JavaScript fallback.
04:17And if they are included, you can be pretty happy and just go on with your CSS animations.
04:22For a more detailed look at how Modernizr works, check out their extensive documentation section.
04:28If supporting older browsers is vital, you should consider creating your animations using
04:32jQuery or another JavaScript only method instead.
04:35Personally, I find it's more work than its worth to maintain both a CSS and JavaScript version of the same content.
04:42In this situation stick with JavaScript for now and consider moving to a CSS animation
04:46solution in the future, when there's a wider support in your audience.
Collapse this transcript
Crafting effective animations
00:00This tutorial will be a little bit less technical.
00:03We'll explore what makes the animation effective, and we'll discuss what we often use animations
00:07for, and where they can make the most effective impact on your design.
00:10I'm sure you've heard the quote, "With great power comes great responsibility."
00:15I doubt a questionable use of CSS animations would really ever cause true harm, but using them
00:20intelligently will go far to making you look smart and having your audience to really connect with your content.
00:25In general, we use animations in user interface design to do one of the following, to attract
00:31our users' attention to a certain area, like it or not movement really grabs our attention,
00:35and that's one of the reasons banner ads are so hard to ignore.
00:38We might also use it to hide or reveal content in a slider or drawer type UI setting.
00:44We might use it to illustrate or demonstrate a product feature.
00:46And we might use it to provide cues and clues for our users, to make a button stand out
00:51a little more, or to show that something has changed in a dynamic interface.
00:55In these situations, it's of the utmost importance that the animations we create work for our users not against them.
01:01Usually this means keeping them slightly understated and making sure they react immediately when
01:06they happen in response to users' input.
01:08No one wants to wait for the button they just clicked to reveal the content they've requested.
01:13All the items in that list are factors that determine how your interface behaves.
01:17But animations can also greatly affect the experience of your site or app.
01:21Animations can be used as an additional design detail to convey your overall concept and the personality of your site.
01:27Imagine a site about gardening that uses natural and earthy colors throughout the design.
01:32Adding animations to this site that use soft organic feeling movement would further the
01:36natural and botanical concepts used in the design.
01:39Conversely, fast overly bouncy animations would really distract from the concept.
01:44Let's look at a couple of real life examples of this as well.
01:48In YouTube's site 1 Hour Per Second, the animations that they use are nice and quirky and have
01:53a little bit of bounce to them and kind of really fit with the illustration style which
01:57is also a little bit imperfect and definitely fits with the copy, which is a little bit funny.
02:01So these quirky animations really fit in with the overall concept.
02:06Another real life example that's a little bit different would be the Google+ Circle Editor.
02:10This one is a little bit different where the animation actually happens as part of the interface you're interacting with.
02:16When you select a group of friends and then go to move them, they're animated in place under a paper clip.
02:21So, these both add a little bit of interest to the UI, but it also is very informative.
02:25It shows you, you have three people, all selected in one place, and you're ready to move them
02:30together and drop them in anyone of your circles.
02:33The best way to know for sure if your animation fits with your design is to test how it feels.
02:38Build a prototype and try using it yourself. Have other team members or friends try it too.
02:44How does it feel to you? What message do you get from it?
02:46Does it seem too fast or too slow or too laggy?
02:49Iterate on this prototype until you get something that feels right for your design.
02:53It's nearly impossible to know what will work when it comes to animating, until you try it.
02:58The more you try, the more you'll get a feel for developing effective and elegant animations.
Collapse this transcript
5. Tools for Creating CSS Animations
Helpful online tools for creating and building animations
00:00The web design community is full of helpful people, and because of that we've got a lot
00:04of great online tools to help you create CSS animations.
00:08We've touched on some of these tools throughout the course, so here's the full list of the ones I find the most useful.
00:14CSS3Please and CSS3Generator are wonderful online tools for generating correct animation
00:19code and previewing what it might look like. Both allow you to edit values live on the page.
00:25For example, things like your duration, and actually see the results live after you type them.
00:32Once you've got something you like, you can just copy and paste the code snippet and use it however you'd like.
00:37CSS3Generator works in a similar way, but you just first need to pick what you want
00:41to work from, from their list, and then it gives you a series of options to work with.
00:46The two best tools out there for generating your own cubic Bezier functions for easing
00:50and timing functions are Ceaser and cubic-bezier.com.
00:54Ceaser allows you to pick from a list of common presets based on things like the popular Penner
00:58Easing Equations as well as others.
01:00And you can also edit the graph yourself for custom results.
01:03So we can pick from this entire list of presets and also edit the handles to create our own curve.
01:10I use Ceaser the most when I have a particular type of motion in mind, and I'm looking for
01:14the equation that will help to find it the best.
01:16Cubic-bezier.com doesn't have as many preview options, but it does give you the ability
01:21to compare the easing results from two different graphs, which is really helpful.
01:25In this example, I would be comparing the ease keyword graph against the linear keyword graph.
01:31I can just hit GO and see how they would work against each other.
01:34You can also create custom graphs here and then bookmark them for sharing with your team later. That's very handy.
01:39And then there is the URL that reflects the same numbers I have up here, and that's how you can bookmark them.
01:45If you hate working from a blank slate as much as I do, there are tools like animate.css
01:49and animatable to help you start with something more than just a blank file.
01:54Animate.css is both a live demo and a downloadable library with CSS animation presets that you can use in your own projects.
02:02The best way to use these is at the starting point.
02:05Find one that's close to the effect that you want to create and edit it to fit what you have in mind.
02:10Tweaking some existing code is often easier than starting from scratch.
02:14Animatable is a visual and functional account of the CSS properties that can be animated.
02:19It's a long list, but the site is a lot more fun than just reading the W3C spec.
02:25Last, but certainly not least, if you're ever curious about the proper use of animation
02:29syntax or the reasoning behind any of it, CSS tricks and the W3C spec are where to go.
02:35It's true that the W3C spec on animations is a very dry read, but the in-depth explanations
02:41behind the spec are pretty useful.
02:43For friendlier explanations, CSS tricks have been covered.
02:46There's an entire section in the Almanac on animations and many of the post include tips
02:50on using animations and other CSS3 properties in the real world.
02:54These sites are all ones that I use regularly when working on a project with animations,
02:58and I hope you find them helpful too.
Collapse this transcript
Conclusion
Next steps
00:00I'm really excited about the options that CSS animations open up for us, which means
00:04there's still a whole lot of potential to be discovered.
00:07For next steps, as you continue working with CSS animations, I encourage you to experiment
00:12and play as much as possible, really have fun with it.
00:14There are a handful of lynda.com courses you might want to check out, if you haven't already.
00:19This would include CSS with LESS and Sass, which we touched on just briefly, also CSS: Transitions and Transforms.
00:26And if you wanted to learn a little more about JavaScript, there's jQuery Essential Training
00:31as well as JavaScript Essential Training.
Collapse this transcript


Suggested courses to watch next:

CSS: Transitions and Transforms (2h 25m)
Joseph Lowery


Create Animated Rollovers with jQuery (29m 26s)
Chris Converse

CSS: Frameworks and Grids (3h 27m)
James Williamson


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,141 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,025 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

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.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

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

   
submit Lightbox submit clicked