Cara menggunakan css animation transition

The idea of CSS transitions is simple. We describe a property and how its changes should be animated. When the property changes, the browser paints the animation.

Show

    That is, all we need is to change the property, and the fluid transition will be done by the browser.

    For instance, the CSS below animates changes of

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    1 for 3 seconds:

    .animated {
      transition-property: background-color;
      transition-duration: 3s;
    }

    Now if an element has

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    2 class, any change of
    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    1 is animated during 3 seconds.

    Click the button below to animate the background:

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>

    There are 4 properties to describe CSS transitions:

    • stripe.onclick = function() {
        stripe.classList.add('animate');
      };
      4
    • stripe.onclick = function() {
        stripe.classList.add('animate');
      };
      5
    • stripe.onclick = function() {
        stripe.classList.add('animate');
      };
      6
    • stripe.onclick = function() {
        stripe.classList.add('animate');
      };
      7

    We’ll cover them in a moment, for now let’s note that the common

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    8 property allows declaring them together in the order:
    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    9, as well as animating multiple properties at once.

    For instance, this button animates both

    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    0 and
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    1:

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>

    Now, let’s cover animation properties one by one.

    In

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    4, we write a list of properties to animate, for instance:
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    3,
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    4,
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    5,
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    0. Or we could write
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    7, which means “animate all properties”.

    Do note that, there are properties which can not be animated. However, most of the generally used properties are animatable.

    In

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    5 we can specify how long the animation should take. The time should be in : in seconds
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    9 or milliseconds
    <!doctype html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <link rel="stylesheet" href="style.css">
    </head>
    
    <body>
    
      Click below to animate:
    
      <div id="digit"><div id="stripe">0123456789</div></div>
    
      <script src="script.js"></script>
    </body>
    
    </html>
    0.

    In

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    7 we can specify the delay before the animation. For instance, if
    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    7 is
    <!doctype html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <link rel="stylesheet" href="style.css">
    </head>
    
    <body>
    
      Click below to animate:
    
      <div id="digit"><div id="stripe">0123456789</div></div>
    
      <script src="script.js"></script>
    </body>
    
    </html>
    3 and
    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    5 is
    <!doctype html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <link rel="stylesheet" href="style.css">
    </head>
    
    <body>
    
      Click below to animate:
    
      <div id="digit"><div id="stripe">0123456789</div></div>
    
      <script src="script.js"></script>
    </body>
    
    </html>
    5, then the animation starts 1 second after the property change and the total duration will be 2 seconds.

    Negative values are also possible. Then the animation is shown immediately, but the starting point of the animation will be after given value (time). For example, if

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    7 is
    <!doctype html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <link rel="stylesheet" href="style.css">
    </head>
    
    <body>
    
      Click below to animate:
    
      <div id="digit"><div id="stripe">0123456789</div></div>
    
      <script src="script.js"></script>
    </body>
    
    </html>
    7 and
    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    5 is
    <!doctype html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <link rel="stylesheet" href="style.css">
    </head>
    
    <body>
    
      Click below to animate:
    
      <div id="digit"><div id="stripe">0123456789</div></div>
    
      <script src="script.js"></script>
    </body>
    
    </html>
    5, then animation starts from the halfway point and total duration will be 1 second.

    Here the animation shifts numbers from

    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }
    0 to
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }
    1 using CSS
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }
    2 property:

    Hasil

    script.js

    style.css

    index.html

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };

    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }

    <!doctype html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <link rel="stylesheet" href="style.css">
    </head>
    
    <body>
    
      Click below to animate:
    
      <div id="digit"><div id="stripe">0123456789</div></div>
    
      <script src="script.js"></script>
    </body>
    
    </html>

    The

    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }
    3 property is animated like this:

    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }

    In the example above JavaScript adds the class

    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }
    4 to the element – and the animation starts:

    stripe.classList.add('animate');

    We could also start it from somewhere in the middle of the transition, from an exact number, e.g. corresponding to the current second, using a negative

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    7.

    Here if you click the digit – it starts the animation from the current second:

    Hasil

    script.js

    style.css

    index.html

    stripe.onclick = function() {
      let sec = new Date().getSeconds() % 10;
      stripe.style.transitionDelay = '-' + sec + 's';
      stripe.classList.add('animate');
    };

    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    0

    JavaScript does it with an extra line:

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    1

    The timing function describes how the animation process is distributed along its timeline. Will it start slowly and then go fast, or vice versa.

    It appears to be the most complicated property at first. But it becomes very simple if we devote a bit time to it.

    That property accepts two kinds of values: a Bezier curve or steps. Let’s start with the curve, as it’s used more often.

    The timing function can be set as a Bezier curve with 4 control points that satisfy the conditions:

    1. First control point:
      #stripe.animate {
        transform: translate(-90%);
        transition-property: transform;
        transition-duration: 9s;
      }
      6.
    2. Last control point:
      #stripe.animate {
        transform: translate(-90%);
        transition-property: transform;
        transition-duration: 9s;
      }
      7.
    3. For intermediate points, the values of
      #stripe.animate {
        transform: translate(-90%);
        transition-property: transform;
        transition-duration: 9s;
      }
      8 must be in the interval
      #stripe.animate {
        transform: translate(-90%);
        transition-property: transform;
        transition-duration: 9s;
      }
      9,
      stripe.classList.add('animate');
      0 can be anything.

    The syntax for a Bezier curve in CSS:

    stripe.classList.add('animate');
    1. Here we need to specify only 2nd and 3rd control points, because the 1st one is fixed to
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }
    6 and the 4th one is
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }
    7.

    The timing function describes how fast the animation process goes.

    • The
      #stripe.animate {
        transform: translate(-90%);
        transition-property: transform;
        transition-duration: 9s;
      }
      8 axis is the time:
      #stripe.animate {
        transform: translate(-90%);
        transition-property: transform;
        transition-duration: 9s;
      }
      0 – the start,
      stripe.classList.add('animate');
      6 – the end of
      stripe.onclick = function() {
        stripe.classList.add('animate');
      };
      5.
    • The
      stripe.classList.add('animate');
      0 axis specifies the completion of the process:
      #stripe.animate {
        transform: translate(-90%);
        transition-property: transform;
        transition-duration: 9s;
      }
      0 – the starting value of the property,
      stripe.classList.add('animate');
      6 – the final value.

    The simplest variant is when the animation goes uniformly, with the same linear speed. That can be specified by the curve

    stripe.onclick = function() {
      let sec = new Date().getSeconds() % 10;
      stripe.style.transitionDelay = '-' + sec + 's';
      stripe.classList.add('animate');
    };
    1.

    Here’s how that curve looks:

    …As we can see, it’s just a straight line. As the time (

    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }
    8) passes, the completion (
    stripe.classList.add('animate');
    0) of the animation steadily goes from
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }
    0 to
    stripe.classList.add('animate');
    6.

    The train in the example below goes from left to right with the permanent speed (click it):

    Hasil

    style.css

    index.html

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    2

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    3

    The CSS

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    8 is based on that curve:

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    4

    …And how can we show a train slowing down?

    We can use another Bezier curve:

    stripe.onclick = function() {
      let sec = new Date().getSeconds() % 10;
      stripe.style.transitionDelay = '-' + sec + 's';
      stripe.classList.add('animate');
    };
    7.

    The graph:

    As we can see, the process starts fast: the curve soars up high, and then slower and slower.

    Here’s the timing function in action (click the train):

    Hasil

    style.css

    index.html

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    5

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    3

    CSS:

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    7

    There are several built-in curves:

    stripe.onclick = function() {
      let sec = new Date().getSeconds() % 10;
      stripe.style.transitionDelay = '-' + sec + 's';
      stripe.classList.add('animate');
    };
    8,
    stripe.onclick = function() {
      let sec = new Date().getSeconds() % 10;
      stripe.style.transitionDelay = '-' + sec + 's';
      stripe.classList.add('animate');
    };
    9,
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    0,
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    1 and
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    2.

    The

    stripe.onclick = function() {
      let sec = new Date().getSeconds() % 10;
      stripe.style.transitionDelay = '-' + sec + 's';
      stripe.classList.add('animate');
    };
    8 is a shorthand for
    stripe.onclick = function() {
      let sec = new Date().getSeconds() % 10;
      stripe.style.transitionDelay = '-' + sec + 's';
      stripe.classList.add('animate');
    };
    1 – a straight line, which we described above.

    Other names are shorthands for the following

    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    5:

    stripe.onclick = function() {
      let sec = new Date().getSeconds() % 10;
      stripe.style.transitionDelay = '-' + sec + 's';
      stripe.classList.add('animate');
    };
    9*
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    0
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    1
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    2
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    00
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    01
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    02
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    03

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    04 – by default, if there’s no timing function,
    stripe.onclick = function() {
      let sec = new Date().getSeconds() % 10;
      stripe.style.transitionDelay = '-' + sec + 's';
      stripe.classList.add('animate');
    };
    9 is used.

    So we could use

    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    1 for our slowing down train:

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    8

    But it looks a bit differently.

    A Bezier curve can make the animation exceed its range.

    The control points on the curve can have any

    stripe.classList.add('animate');
    0 coordinates: even negative or huge ones. Then the Bezier curve would also extend very low or high, making the animation go beyond its normal range.

    In the example below the animation code is:

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    9

    The property

    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    3 should animate from
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    09 to
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    10.

    But if you click the train, you’ll see that:

    • First, the train goes back:
      #digit {
        width: .5em;
        overflow: hidden;
        font: 32px monospace;
        cursor: pointer;
      }
      
      #stripe {
        display: inline-block
      }
      
      #stripe.animate {
        transform: translate(-90%);
        transition-property: transform;
        transition-duration: 9s;
        transition-timing-function: linear;
      }
      3 becomes less than
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      09.
    • Then it goes forward, a little bit farther than
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      10.
    • And then back again – to
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      10.

    Hasil

    style.css

    index.html

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>
    0

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>
    1

    Why it happens is pretty obvious if we look at the graph of the given Bezier curve:

    We moved the

    stripe.classList.add('animate');
    0 coordinate of the 2nd point below zero, and for the 3rd point we made it over
    stripe.classList.add('animate');
    6, so the curve goes out of the “regular” quadrant. The
    stripe.classList.add('animate');
    0 is out of the “standard” range
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
    }
    9.

    As we know,

    stripe.classList.add('animate');
    0 measures “the completion of the animation process”. The value
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    20 corresponds to the starting property value and
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    21 – the ending value. So values
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    22 move the property beyond the starting
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    3 and
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    24 – past the final
    #digit {
      width: .5em;
      overflow: hidden;
      font: 32px monospace;
      cursor: pointer;
    }
    
    #stripe {
      display: inline-block
    }
    
    #stripe.animate {
      transform: translate(-90%);
      transition-property: transform;
      transition-duration: 9s;
      transition-timing-function: linear;
    }
    3.

    That’s a “soft” variant for sure. If we put

    stripe.classList.add('animate');
    0 values like
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    27 and
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    28 then the train would jump out of the range much more.

    But how do we make a Bezier curve for a specific task? There are many tools. For instance, we can do it on the site http://cubic-bezier.com/.

    The timing function

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    29 allows splitting an animation into steps.

    Let’s see that in an example with digits.

    Here’s a list of digits, without any animations, just as a source:

    Hasil

    style.css

    index.html

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>
    2

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>
    3

    We’ll make the digits appear in a discrete way by making the part of the list outside of the red “window” invisible and shifting the list to the left with each step.

    There will be 9 steps, a step-move for each digit:

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>
    4

    In action:

    Hasil

    style.css

    index.html

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>
    5

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>
    6

    The first argument of

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    30 is the number of steps. The transform will be split into 9 parts (10% each). The time interval is automatically divided into 9 parts as well, so
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    31 gives us 9 seconds for the whole animation – 1 second per digit.

    The second argument is one of two words:

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    32 or
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    33.

    The

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    32 means that in the beginning of animation we need to make the first step immediately.

    We can observe that during the animation: when we click on the digit it changes to

    stripe.classList.add('animate');
    6 (the first step) immediately, and then changes in the beginning of the next second.

    The process is progressing like this:

    • <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      36 –
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      37 (first change in the beginning of the 1st second, immediately)
    • <!doctype html>
      <html>
      
      <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="style.css">
      </head>
      
      <body>
      
        Click below to animate:
      
        <div id="digit"><div id="stripe">0123456789</div></div>
      
        <script src="script.js"></script>
      </body>
      
      </html>
      3 –
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      39
    • <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      40 –
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      41
    • (the last second shows the final value).

    The alternative value

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    33 would mean that the change should be applied not in the beginning, but at the end of each second.

    So the process would go like this:

    • <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      36 –
      #stripe.animate {
        transform: translate(-90%);
        transition-property: transform;
        transition-duration: 9s;
      }
      0
    • <!doctype html>
      <html>
      
      <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="style.css">
      </head>
      
      <body>
      
        Click below to animate:
      
        <div id="digit"><div id="stripe">0123456789</div></div>
      
        <script src="script.js"></script>
      </body>
      
      </html>
      3 –
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      37 (first change at the end of the 1st second)
    • <!doctype html>
      <html>
      
      <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="style.css">
      </head>
      
      <body>
      
        Click below to animate:
      
        <div id="digit"><div id="stripe">0123456789</div></div>
      
        <script src="script.js"></script>
      </body>
      
      </html>
      5 –
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      39
    • <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      49 –
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      50

    Here’s

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    51 in action (note the pause between the first digit change):

    Hasil

    style.css

    index.html

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>
    7

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>
    6

    There are also shorthand values:

    • <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      52 – is the same as
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      53. That is, the animation starts immediately and takes 1 step. So it starts and finishes immediately, as if there were no animation.
    • <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      54 – the same as
      <button id="color">Click me</button>
      
      <style>
        #color {
          transition-property: background-color;
          transition-duration: 3s;
        }
      </style>
      
      <script>
        color.onclick = function() {
          this.style.backgroundColor = 'red';
        };
      </script>
      55: make the animation in a single step at the end of
      stripe.onclick = function() {
        stripe.classList.add('animate');
      };
      5.

    These values are rarely used, because that’s not really animation, but rather a single-step change.

    When the CSS animation finishes the

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    57 event triggers.

    It is widely used to do an action after the animation is done. Also we can join animations.

    For instance, the ship in the example below starts to sail there and back when clicked, each time farther and farther to the right:

    The animation is initiated by the function

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    58 that re-runs each time the transition finishes, and flips the direction:

    <button id="growing">Click me</button>
    
    <style>
    #growing {
      transition: font-size 3s, color 2s;
    }
    </style>
    
    <script>
    growing.onclick = function() {
      this.style.fontSize = '36px';
      this.style.color = 'red';
    };
    </script>
    9

    The event object for

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    57 has a few specific properties:

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    60The property that has finished animating. Can be good if we animate multiple properties simultaneously.
    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    61The time (in seconds) that the animation took, without
    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    7.

    We can join multiple simple animations together using the

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    63 CSS rule.

    It specifies the “name” of the animation and rules – what, when and where to animate. Then using the

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    64 property, we can attach the animation to the element and specify additional parameters for it.

    Here’s an example with explanations:

    stripe.onclick = function() {
      stripe.classList.add('animate');
    };
    0

    There are many articles about

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    63 and a detailed specification.

    You probably won’t need

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    63 often, unless everything is in constant motion on your sites.

    CSS animations allow smoothly (or not) animated changes of one or multiple CSS properties.

    They are good for most animation tasks. We’re also able to use JavaScript for animations, the next chapter is devoted to that.

    Limitations of CSS animations compared to JavaScript animations:

    Kelebihan

    • Simple things done simply.
    • Fast and lightweight for CPU.

    kekurangan

    • JavaScript animations are flexible. They can implement any animation logic, like an “explosion” of an element.
    • Not just property changes. We can create new elements in JavaScript as part of the animation.

    The majority of animations can be implemented using CSS as described in this chapter. And the

    <button id="color">Click me</button>
    
    <style>
      #color {
        transition-property: background-color;
        transition-duration: 3s;
      }
    </style>
    
    <script>
      color.onclick = function() {
        this.style.backgroundColor = 'red';
      };
    </script>
    57 event allows JavaScript to be run after the animation, so it integrates fine with the code.