UNIT2
UNIT2
ANIMATION
An animation is nothing more than a visualization of change—a change that
occurs over a period of time.
An HTML animation is any item that appears to be moving or changing on a web
page, usually without any user input. This can be anything from a bouncing ball to
a bank of letters that flash on and off like a neon sign.
You start off with a blue circle that is small and located to the
left of the screen. At the end state, your blue circle now looks
sorta kinda like this:
Based just on the information you have on what our blue circle
looks like in the start and end states, what can you tell is
different?
One change is the position. Our blue circle starts off on the left
side of the screen. It ends up on the right hand side. Another
change is the size. Our circle goes from being small to being
much larger.
Interpolation
Right now, what we have are two discrete states in time. At the
beginning, you have your start state. And the end, you have the
end state. If you were to play this back, this wouldn't be an
animation. In order to make an animation out of what we have,
we need a smooth transition that creates all the intermediate
states. This creation of the intermediate states is known
as interpolation.
You will later see how adding some other ingredients into the
pot such as timing functions (easing functions) can alter how
the interpolation works, but we'll get there later. For now, just
revel in this simplified generalization of what makes up an
animation, put on your best party clothes, and get ready to
meet the three flavors of animation that you will end up using.
Animations in HTML
2. CSS Transitions
If you want full control over what your animation does right
down to how it interpolates between two states, you can use
JavaScript:
There are a lot of cool things you can do when you opt-out of
the interpolation the browser does for you, and you'll get a
good dose of that in the tutorials that look at JavaScript
Animations in greater detail.
CSS ANIMATIONS
@keyframes
animation-name
animation-duration
animation-delay
animation-iteration-count
animation-direction
animation-timing-function
animation-fill-mode
animation
To use CSS animation, you must first specify some keyframes for the
animation.
Keyframes hold what styles the element will have at certain times.
In the example above we have specified when the style will change by using the
keywords "from" and "to" (which represents 0% (start) and 100% (complete)).
It is also possible to use percent. By using percent, you can add as many style
changes as you like.
The following example will change the background-color of the <div> element
when the animation is 25% complete, 50% complete, and again when the
animation is 100% complete:
Delay an Animation
The following example has a 2 seconds delay before starting the animation:
Example
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-delay: 2s;
}
Set How Many Times an Animation Should Run
The following example will run the animation 3 times before it stops:
Example
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count:3;
}
INFINITE loop
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: infinite;
}
The following example will run the animation in reverse direction (backwards):
Example
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-direction: reverse;
}
Specify the Speed Curve of the Animation:
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 (before it starts, after it ends, or both).
none - Default value. Animation will not apply any styles to the element
before or after it is executing
forwards - The element will retain the style values that is set by the last
keyframe (depends on animation-direction and animation-iteration-count)
backwards - The element will get the style values that is set by the first
keyframe (depends on animation-direction), and retain this during the
animation-delay period
both - The animation will follow the rules for both forwards and backwards,
extending the animation properties in both directions
The following example lets the <div> element retain the style values from the last
keyframe when the animation ends:
Example
div {
width: 100px;
height: 100px;
background: red;
position: relative;
animation-name: example;
animation-duration: 3s;
animation-fill-mode: forwards;
}
The following example lets the <div> element get the style values set by the first
keyframe before the animation starts (during the animation-delay period):
Example
div {
width: 100px;
height: 100px;
background: red;
position: relative;
animation-name: example;
animation-duration: 3s;
animation-delay: 2s;
animation-fill-mode: backwards;
}
The following example lets the <div> element get the style values set by the first
keyframe before the animation starts, and retain the style values from the last
keyframe when the animation ends:
Example
div {
width: 100px;
height: 100px;
background: red;
position: relative;
animation-name: example;
animation-duration: 3s;
animation-delay: 2s;
animation-fill-mode: both;
}
The animation-timing-function property specifies the speed curve of the animation.
ease - Specifies an animation with a slow start, then fast, then end slowly
(this is default)
linear - Specifies an animation with the same speed from start to end
ease-in - Specifies an animation with a slow start
ease-out - Specifies an animation with a slow end
ease-in-out - Specifies an animation with a slow start and end
cubic-bezier(n,n,n,n) - Lets you define your own values in a cubic-bezier
function
The following example shows some of the different speed curves that can be used:
Example
#div1 {animation-timing-function: linear;}
#div2 {animation-timing-function: ease;}
#div3 {animation-timing-function: ease-in;}
#div4 {animation-timing-function: ease-out;}
#div5 {animation-timing-function: ease-in-out}
CSS TRANSITIONS
CSS transitions allows you to change property values smoothly, over a given
duration.
transition
transition-delay
transition-duration
transition-property
transition-timing-function
o create a transition effect, you must specify two things:
Note: If the duration part is not specified, the transition will have no effect,
because the default value is 0.
The following example shows a 100px * 100px red <div> element. The <div>
element has also specified a transition effect for the width property, with a duration
of 2 seconds:
Example
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s;
}
Property Description
transition-property Specifies the name of the CSS property the transition effect i
ease - specifies a transition effect with a slow start, then fast, then end
slowly (this is default)
linear - specifies a transition effect with the same speed from start to end
ease-in - specifies a transition effect with a slow start
ease-out - specifies a transition effect with a slow end
ease-in-out - specifies a transition effect with a slow start and end
cubic-bezier(n,n,n,n) - lets you define your own values in a cubic-bezier
function
The following example shows some of the different speed curves that can be used:
Example
#div1 {transition-timing-function: linear;}
#div2 {transition-timing-function: ease;}
#div3 {transition-timing-function: ease-in;}
#div4 {transition-timing-function: ease-out;}
#div5 {transition-timing-function: ease-in-out;}
Delay the Transition Effect
The transition-delay property specifies a delay (in seconds) for the transition effect.
Example
div {
transition-delay: 1s;
}
Background Properties
Textual style Properties
Transition Properties
List Properties
Outline and Border Properties
These are the main 5 on our rundown of most frequently utilized shorthand
properties. There are others also, even in CSS3.