We launched a new IT training category! Check out the 140+ courses now.

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

Playing and pausing an animation on hover

From: CSS: Animations

Video: Playing and pausing an animation on hover

In previous tutorials, we created animations that played regardless of user input, as animations tend to do. CSS is limited in how much they can respond to user input on its own without help from something like JavaScript. But we can still use that input a little bit here and there. In this tutorial, we're going to create a button that cycles its background color in response to the mouse pointer hovering over it and then pauses the animation when the mouse leaves, just like this button we're looking at right now. When we hover over it, the animation plays and our background color cycle in an interesting little glowing pattern, and when we hover off the animation pauses and stops.

Playing and pausing an animation on hover

In previous tutorials, we created animations that played regardless of user input, as animations tend to do. CSS is limited in how much they can respond to user input on its own without help from something like JavaScript. But we can still use that input a little bit here and there. In this tutorial, we're going to create a button that cycles its background color in response to the mouse pointer hovering over it and then pauses the animation when the mouse leaves, just like this button we're looking at right now. When we hover over it, the animation plays and our background color cycle in an interesting little glowing pattern, and when we hover off the animation pauses and stops.

You can follow along with the exercise files for this tutorial, or you can skip ahead to the final to see how everything works out. Let's go to Coda and take a look at the HTML behind this example. It's a very simple HTML, once again, and we have a div with the class of button and within that we have a link with the text of the button called Contact Me. For right now, our link is going nowhere because this is just an example. But in a real situation, this link would obviously be going somewhere. And if we take a look at our starting styles, we have a basic setup, setting a background color and the shape of our button, as well as the styles to define how our link should look inside our button.

So let's get started by creating our animation. For this example, we're going to animate between a few background colors to give the button a unique glow effect. It's definitely something different than we could get with the transition or other hover effects. So it's unique to animations. First, we'll define our keyframes. I am going to name this animation glow since that's more or less what we're going to be doing here. I have picked a few shades of blue and bluish- greens that I'd like to cycle through for my button. I'll assign each has a background color property at various percentage keyframes.

So I'll start with my 0% keyframe with the background color that I've already picked. So at our 0% keyframe, we will add the background color of this shade of blue that I've already picked ahead the time and then add a 20% keyframe. We'll add another shade of blue, then at 70%, we will change the background color once again. And then for our last keyframe at 100%, we will set our background color to one final shade.

So I'm writing keyframes like this. It's nice to have all your colors picked out ahead the time whether you have a specific palette in Photoshop or something similar. I picked the percentages based on what looked best to me. But really, it's just kind of a made-up effect. So, if certain other percentages or maybe more colors or more keyframes looks better for you, feel free to add them. Now that we have created our animation, let's assign this animation to our button. We'll use the shorthand to assign this animation to our button. So we'll start by adding the animation property. We will use the glow animation, since that's the one we just made for our animation name.

For our duration, we'll select 3 seconds, and that's just a duration I've picked as looking good for cycling through those colors. We are going to select the animation timing function of ease just to give it a nice in and out kind of feel to it. We will repeat this animation an infinite number of times. So our animation iteration count is infinite, and our animation direction I'm going to set to alternate. And the reason I am doing this is so that our animation will play forwards through our keyframes then backwards to our keyframes. And that will stop it from having a hard cut between the 100% and 0% keyframe, because it will keep going back and forth so it will be a nice smooth transition through all our colors.

So let's save everything and preview our animation right now. So we'll go over to our Browser, Preview our animation, and you'll see our button is starting to cycle colors slowly over the 3 seconds. Going forwards first then backwards, and sometimes the change is faster or slower, that all depends on how our keyframes are set up. So everything is looking good. Let's get on to adding the pause and play functionality to our animation. So back to our CSS in Coda, we're going to add a couple of things. First, we'll add one more line to our button class, and what we're going to do set the animation play state to paused initially.

So we have our animation-play-state, and we'll set that to paused. Animation-play-state can take two different values. The animation-play-state can be paused or running and those work just like they sound. If your play-state is paused, nothing is happening. It's paused like you hit the Pause button. And if your animation-play-state is running, it's working like you hit the Play button. Our animation is now paused by default, and if we previewed our button again now, nothing would happen at all because it's paused. Paused animations, however, aren't really anywhere near as much fun as ones that plays.

So let's add a condition for when our animation will actually play. To do that, we'll add a rule for button:hover, and within that rule, we'll add another animation-play-state property. So I'll just copy that from up here because it's really long to type, and this time, I will set it to running. So what this means is that the animation on our button will be paused initially, but then when we hover over the button, the animation-play- state will be switched to running and by default, animations are always in a play-state of running unless we tell it otherwise.

Let's go to our browser and see what our button looks like right now. When we refresh the page, initially nothing is happening. Our button is just hanging out like a regular button. But if we hover over it, suddenly the animation starts playing, and we see that cycling background color. If we hover off of it, the animation pauses, and you'll notice there is no hard cut. It just stops right where it was. And if we hover back over it, the animation picks up again right where it paused and keeps going through its cycle. There's some potential here to create a very unique hover effect. In a way, the button appears to behave slightly differently each time you hover over it, and it is a more complex effect than we could have gotten from using a transition or just a regular CSS hover.

One more time we'll go back to Coda, and in our CSS, we'll add our vendor prefixes since we're happy with our animation right now. Now that we have our vendor prefixes in place, this animation will play in non-WebKit browsers too. CSS animations triggered by hovers are best suited for subtle and non-essential animations because in older browsers, we wouldn't actually be able to see this effect at all. For example, if we were looking at this button in a browser that doesn't support animations at all, we would just see the static button and really nothing would be lost. So it's important not to include any essential information in animations like this.

For even better control over animations triggered by interaction, you can combine CSS animations with JavaScript, using JavaScript for the logic and CSS for the animations. We'll look at that a little bit later in this course, and it's something to look into if you're really interested in CSS animations.

Show transcript

This video is part of

Image for CSS: Animations
CSS: Animations

29 video lessons · 12052 viewers

Val Head
Author

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

Start learning today

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

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

What are exercise files?

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

Can I take this course without the exercise files?

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

Become a member Download sample files See plans and pricing

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

Exercise files video

How to use exercise files.

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


Exercise files

Exercise files video

How to use exercise files.

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

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

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

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


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Animations.

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


OK
Become a member to add this course to a playlist

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

Get started

Already a member ?

Become a member to like this course.

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

Get started

Already a member?

Exercise files

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

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

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

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

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

Continue to classic layout Stay on new layout
Exercise files

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

Mark videos as unwatched

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

Control your viewing experience

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

Interactive transcripts

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

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


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

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

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

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

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

   
submit Lightbox submit clicked
Terms and conditions of use

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