Return to Video

Animating styles with CSS animations (Video Version)

  • 0:00 - 0:04
    Okay, so you might be debating right now
  • 0:04 - 0:07
    whether to use "set interval" or
    "request animation frame"
  • 0:07 - 0:09
    for what you what to animate,
  • 0:09 - 0:13
    but I'm going to go ahead and throw one
    more option in the mix.
  • 0:13 - 0:17
    You see, in Chrome, Firefox, and IE10 Plus
  • 0:17 - 0:20
    there's actually a way in CSS
  • 0:20 - 0:21
    to make animations
  • 0:21 - 0:24
    without writing any Javascript at all.
  • 0:25 - 0:28
    Let's comment out the code we just wrote
  • 0:28 - 0:31
    and try it for the "Oh noes" animation.
  • 0:31 - 0:36
    Just put a big multiline comment
    around all that.
  • 0:37 - 0:41
    So we start by adding a "" type to
    the page
  • 0:41 - 0:45
    and then adding something that looks
    like a CSS rule,
  • 0:45 - 0:48
    but is actually our animation definition.
  • 0:48 - 0:51
    So we write, "@keyframes" and then
  • 0:51 - 0:55
    a name for animation: "getbigger,"
  • 0:55 - 0:57
    and then curly brackets.
  • 0:57 - 1:01
    To do a simple animation that goes from
    one state to another state,
  • 1:01 - 1:07
    We'll define 'from' and 'to' states
  • 1:07 - 1:10
    Inside 'from,' we'll write what the
    starting
  • 1:10 - 1:12
    CSS property should be.
  • 1:12 - 1:15
    And remember how we set it to 50px
  • 1:15 - 1:16
    at the beginning.
  • 1:16 - 1:18
    Inside 'to,' we'll write what the
  • 1:18 - 1:21
    end property should be.
  • 1:21 - 1:24
    And here, maybe, it was 300px,
  • 1:24 - 1:26
    is what we ended at. Remember?
  • 1:26 - 1:28
    Now that we've defined an animation
  • 1:28 - 1:31
    we need to tell the browser what element
  • 1:31 - 1:33
    should actually use this animation.
  • 1:33 - 1:36
    So, actually, we'll add a normal CSS rule
  • 1:36 - 1:38
    for "Oh noes."
  • 1:39 - 1:43
    And inside here, we specify the animation
    name:
  • 1:44 - 1:46
    that's "getbigger."
  • 1:47 - 1:51
    And then animation duration: 3 seconds.
  • 1:53 - 1:55
    Depending on what browser you're in,
  • 1:55 - 1:56
    some of you right now be thinking
  • 1:56 - 1:59
    "Woah! Sweet it's working!"
  • 1:59 - 2:01
    In other browsers though, like Safari
    or Chrome,
  • 2:01 - 2:03
    it's probably not working.
  • 2:03 - 2:05
    That's because there is something called
  • 2:05 - 2:07
    "vendor prefixes."
  • 2:10 - 2:14
    Sometimes, a browser decides to support
  • 2:14 - 2:17
    a fancy new feature, but they put
  • 2:17 - 2:19
    a "vendor prefix" on that feature
  • 2:19 - 2:22
    to indicate that it might change later.
  • 2:22 - 2:26
    This is just their browser's attempt
    at the feature.
  • 2:26 - 2:29
    To make this work in Chrome,
    it's not already,
  • 2:29 - 2:31
    we have to replicate everything we just
  • 2:31 - 2:34
    did and stick "-webkit-" in front.
  • 2:35 - 2:37
    So we need to replicate this part
  • 2:38 - 2:41
    and put "-webkit-" here
  • 2:42 - 2:45
    and then here, we'll replicate this
  • 2:45 - 2:50
    and put "-webkit-", "-webkit-."
  • 2:50 - 2:53
    Woah, sweet! Now it's getting bigger
  • 2:53 - 2:55
    for every browser.
  • 2:55 - 2:58
    Hopefully, by the time you watch this
    talk through,
  • 2:58 - 3:00
    those "vendor prefixes" won't
    be necessary,
  • 3:00 - 3:02
    but it's good to know that they exist
  • 3:02 - 3:06
    because you might need to use them
    for some other feature one day.
  • 3:08 - 3:10
    There's also one more way in CSS
  • 3:10 - 3:12
    to make animations
  • 3:12 - 3:14
    and that's with a transition property.
  • 3:15 - 3:17
    It tells the browser how to transition
  • 3:17 - 3:20
    smoothly from one property to the next
  • 3:21 - 3:24
    Let's say we want the font size of the
  • 3:24 - 3:27
    time left to get bigger
  • 3:27 - 3:28
    when you mouse over it.
  • 3:28 - 3:30
    We could do all that in Javascript by
  • 3:30 - 3:32
    assigning an event listener for the
  • 3:32 - 3:34
    mouse over event. Then using a request
  • 3:34 - 3:36
    animation frame to increase the font size
  • 3:36 - 3:38
    property each time.
  • 3:38 - 3:42
    But, we can also do that entirely in CSS.
  • 3:42 - 3:43
    Let's think.
  • 3:43 - 3:45
    How would we normally change the font size
  • 3:45 - 3:48
    to be bigger, when hovering, in CSS?
  • 3:49 - 3:51
    We can do that with a hover rule.
  • 3:51 - 3:55
    We say "#countdown:hover," and then
  • 3:55 - 3:59
    "font-size: 150px"
  • 4:00 - 4:02
    Okay, and now we just need to tell the
  • 4:02 - 4:06
    browser to transition the font size
    property,
  • 4:06 - 4:09
    how much time to transition it over, and
  • 4:09 - 4:11
    what timing function to use.
  • 4:12 - 4:17
    So we say "transition:
    font-size 1s linear;".
  • 4:19 - 4:22
    Now, you should pause the talk through and
  • 4:22 - 4:24
    try hovering over the text
  • 4:24 - 4:26
    to see what happens.
  • 4:26 - 4:29
    If you're in Chrome, Firefox, or IE10 Plus
  • 4:29 - 4:31
    then it should get bigger smoothly
  • 4:31 - 4:34
    and you don't need any "vendor prefixes"
  • 4:34 - 4:35
    for this technique.
  • 4:35 - 4:37
    There is a whole lot that you can do with
  • 4:37 - 4:39
    CSS animations and transitions
  • 4:39 - 4:41
    and browsers are pretty good at
  • 4:41 - 4:43
    rendering them quickly. So I encourage you
  • 4:43 - 4:45
    to explore both of them a lot more.
Title:
Animating styles with CSS animations (Video Version)
Description:

more » « less
Video Language:
English
Duration:
04:47

English subtitles

Revisions