Categories
Web development

JavaScript HTML DOM/Animations

Learn how to create HTML animations using JavaScript.

JavaScript HTML DOM Animations
  1. Create an Animation Container – all animations should be relative to a container element.
<div id ="content">
  <div id ="animate">The animation will go here.</div>
</div>

2. Style the Elements:

The container element should be created with style =“position: relative”

The animation element should be created with style =“position: absolute”.

<!Doctype html>
<html>
<style>
#content {
  width: 400px;
  height: 400px;
  position: relative;
  background-color: #e9c46a;
}
#animate {
  width: 50px;
  height: 50px;
  position: absolute;
  background-color: #264653;
}
</style>
<body>

<div id="content">
<div id="animate"></div>
</div>

</body>
</html>

Output:


3. Animation Code – JavaScript animations are done by programming gradual changes in an element’s style.

The changes are called by a timer. When the timer interval is small, the animation looks continuous.

function myAnimation() {
  var elem = document.getElementById("animate");   
  var pos = 0;
  var id = setInterval(frame, 5);
  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
      pos++; 
      elem.style.top = pos + "px"; 
      elem.style.left = pos + "px"; 
    }
  }
}

Example:

<!DOCTYPE html>
<html>
<style>
#content {
  width: 400px;
  height: 400px;
  position: relative;
  background-color: #e9c46a;
}
#animate {
  width: 50px;
  height: 50px;
  position: absolute;
  background-color: #264653;
}
</style>
<body>

<p><button onclick="myAnimation()">Start!</button></p> 

<div id ="content">
  <div id ="animate"></div>
</div>

<script>
function myAnimation() {
  var elem = document.getElementById("animate");   
  var pos = 0;
  var id = setInterval(frame, 5);
  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
      pos++; 
      elem.style.top = pos + "px"; 
      elem.style.left = pos + "px"; 
    }
  }
}
</script>

</body>
</html>

Output:


Enjoy coding!

Read also:

JavaScript Introduction

JavaScript HTML DOM

Categories
Web development

CSS Transition VS. CSS Animation

You might think transitions and animations refer to the same thing, but they don’t. The CSS Transition can not change CSS properties which CSS Animation can do.

CSS transiton vs CS animation

CSS Transitions allow changing property values very smoothly, over a given duration. To create a transition effect, you must specify two things:

  1. The CSS property you want to add an effect to
  2. The duration of the effect

Note: If the duration is not specified, the transition will not occur (default 0).

Example:

Hover over the div element (square) below, to see the transition effect (<div> element is changing the width from 100px to 200px). 

<!DOCTYPE html>
<html>
<head>
<style>
.square {
  width: 100px;
  height: 100px;
  background: #2a9d8f;
  transition: width 1s;
}

.square:hover {
  width: 200px;
}
</style>
</head>
<body>

<p>Hover over the square:</p>
<div class="square"></div>

</body>
</html>

Output:

Hover over the square:

Most often you can see transitions used on hover states, or when information on a page is added or removed. The hover states may be a subtle change in font color and information on the page may fade from invisible to visible. 

CSS Animations are a more powerful alternative to transitions. Rather than rely on a change from one beginning state to an end state. 

Where a transition goes only from A to B, the animation can go from A, B, C to D. Or any number of stages as needed. (To see the basics of CSS Animations click here.)

Example:

<!DOCTYPE html>
<html>
<head>
<style>
.square {
  width: 100px;
  height: 100px;
  background-color: #2a9d8f;
  position: relative;
  animation-name: example-2;
  animation-duration: 3s;
}

@keyframes example-2 {
  0%   {left:0px; top:0px;}
  25%  {left:100px; top:0px;}
  50%  {left:100px; top:100px;}
  75%  {left:0px; top:100px;}
  100% {left:0px; top:0px;}
}
</style>
</head>
<body>

<div class="square"></div>

</body>
</html>   

Output:

Click on the square to repeat the animation.




The CSS Transition is for creating a smooth transition from one state to another, and the CSS Animation is for a more complex series of movements. 

Enjoy coding!

Read also:

How to add custom fonts to HTML?

CSS cubic-bezier() Function

CSS Sticky Element

Categories
Web development

Introduction to CSS Animation

Animation is a great way to make your web page more attractive. CSS animation property allows the animation of HTML elements without using JavaScript.

CSS Animation Introduction

What is a CSS Animation?

An animation lets an element gradually change from one style to another one. You can change as many CSS  properties you want, as many times you want. To use CSS animation, you must first specify some keyframes for the animation. 

The CSS animation property is shorthand of the following properties:

@keyframes

animation-name

animation-duration

animation-delay

animation-iteration-count

animation-direction

animation-timing-function

animation-fill-mode

The @keyframes Rule

The main component of CSS Animation is @keyframes – the CSS rule where animation is created. Inside @keyframes, you can define the stages and settings of the animation.

Example:

<!DOCTYPE html>
<html>
<head>
<style>
.example-1 {
  width: 100px;
  height: 100px;
  background-color: #2a9d8f;
  animation-name: example-1;
  animation-duration: 4s;
}

@keyframes example-1 {
  from {background-color: #2a9d8f;}
  to {background-color: #e9c46a;}
}
</style>
</head>
<body>

<div class="example-1"></div>

</body>
</html>

Note: The animation will finish in 4 seconds. It will gradually change the background color of the <div> element from “green” to “yellow”. After the animation, the square changes back to its original color.

Output:


Click on the square to repeat the animation.

The animation-duration property defines how long time an animation should take to complete. If the animation-duration property is not specified, no animation will occur, because the default value is 0 seconds.

The following example will change the background and the position of the <div> element when the animation is 50% complete, and again when the animation is 100% complete:

<!DOCTYPE html>
<html>
<head>
<style>
.square {
  width: 100px;
  height: 100px;
  background-color: #2a9d8f;
  position: relative;
  animation-name: example-2;
  animation-duration: 4s;
}

@keyframes example-2 {
  0%   {background-color:#2a9d8f; left:0px;}
  50%  {background-color:#f4a261; left:200px;}
  100% {background-color:#2a9d8f; left:0px;}
}

</style>
</head>
<body>

<div class="square"></div>

</body>
</html>

Output:


Click on the square to repeat the animation.

The animation-delay property specifies a delay for the start of an animation.

Example (the animation will start with 2 seconds delay):

<!DOCTYPE html>
<html>
<head>
<style>
.square {
  width: 100px;
  height: 100px;
  background-color: #2a9d8f;
  position: relative;
  animation-name: example-3;
  animation-duration: 4s;
  animation-delay: 2s;
}

@keyframes example-3 {
  0%   {background-color:#2a9d8f; left:0px;}
  50%  {background-color:#f4a261; left:200px;}
  100% {background-color:#2a9d8f; left:0px;}
}

</style>
</head>
<body>

<div class="square"></div>

</body>
</html>

Output:


Click on the square to repeat the animation.

The animation-iteration-count property specifies the number of times an animation should run. Using the value “infinite” you will make the animation continue forever.

Example (the animation will repeat 3 times):

<!DOCTYPE html>
<html>
<head>
<style>
.square {
  width: 100px;
  height: 100px;
  background-color: #2a9d8f;
  position: relative;
  animation-name: example-3;
  animation-duration: 4s;
  animation-iteration-count: 3;
}

@keyframes example-3 {
  0%   {background-color:#2a9d8f; left:0px;}
  50%  {background-color:#f4a261; left:200px;}
  100% {background-color:#2a9d8f; left:0px;}
}

</style>
</head>
<body>

<div class="square"></div>

</body>
</html>

Output:


Click on the square to repeat the animation.

The animation-direction property specifies whether the animation should be played forward, backward, or in alternate cycles.

Demo:

The animation-direction property can have the following values:

normal (default) – the animation is played as normal (forward).
reverse – the animation is played in the reverse direction (backward).
alternate – the animation is played in forward first, then backward.
alternate-reverse – the animation is played backward first, then forward.

Example (using the “reverse” value):

<!DOCTYPE html>
<html>
<head>
<style>
.square {
  width: 100px;
  height: 100px;
  background-color: #2a9d8f;
  position: relative;
  left:200px;
  animation-name: example-5;
  animation-duration: 4s;
  animation-direction: reverse; 
}
@keyframes example-5 {
  0%   {background-color:#2a9d8f; left:200px;}
  25%  {background-color:#e76f51; left:0px;}
  50%  {background-color:#e9c46a; left:200px;}
  75%  {background-color:#f4a261; left:0px;}
  100% {background-color:#2a9d8f; left:200px;}
}

</style>
</head>
<body>

<div class="square"></div>

</body>
</html>

Output:


Click on the square to repeat the animation.

The animation-timing-function property specifies the speed curve of animation.

The animation-timing-function property can have the following values:

ease – specifies an animation with a slow start, then fast, then and slowly. This is the default.

linear – specifies an animation with the same speed from start to end.  

ease-in – specifies an animation with a slow start.  

ease-in-out – specifies an animation with a slow start and end. 

CSS animations do not affect an element before the first keyframe is played or after the last keyframe is played. The animation-fill-mode property can override this behavior.

The animation-fill-mode property specifies a style for the target element when the animation is not playing. 

The animation-fill-mode property can have the following values:

none – the default one. The animation will not apply any styles to the element before or after it is executing. 

forwards– the element will retain the style values that are set by the last keyframe.

backwards– the element will get the style values that are set by the first keyframe.

both – the animation will follow the rules for both forwards and backwards, extending the animation properties in both directions. 

Example(square will move 200px to the left and will stay in this position):

<!DOCTYPE html>
<html>
<head>
<style>
.square {
  width: 100px;
  height: 100px;
  background-color: #2a9d8f;
  position: relative;
  animation-name: example-6;
  animation-duration: 2s;
  animation-timing-function: ease;
  animation-fill-mode: forwards;
}

@keyframes example-6 {
  0%   {background-color:#2a9d8f; left:0px;}
  100% {background-color:#2a9d8f; left:200px;}
}

</style>
</head>
<body>

<div class="square"></div>

</body>
</html>

Output:


Click on the square to repeat the animation.

I hope you find it useful.

Enjoy coding!

Read also:

CSS Selectors

CSS Flexbox

CSS Columns