CSS-Animations
Animation: CSS-Animations
What are CSS animations?
View Answer:
Here's a simple CSS animation code example:
@keyframes example {
0% {background-color: red;}
50% {background-color: yellow;}
100% {background-color: blue;}
}
div {
width: 100px;
height: 100px;
animation-name: example;
animation-duration: 4s;
}
In this example, a div
element's background color is animated from red to yellow to blue over a period of 4 seconds. The @keyframes
rule specifies the animation sequence.
What is a CSS keyframe?
View Answer:
@keyframes example {
0% {background-color: red;}
50% {background-color: yellow;}
100% {background-color: blue;}
}
What are the two main properties for controlling CSS animations?
View Answer:
What is the 'animation-delay' property in CSS?
View Answer:
Sure, here's an example illustrating the 'animation-delay' property:
@keyframes example {
0% {background-color: red;}
100% {background-color: blue;}
}
div {
width: 100px;
height: 100px;
animation-name: example;
animation-duration: 4s;
animation-delay: 2s; /* animation-delay property */
}
In this example, the animation on the div
element's background color will start after a delay of 2 seconds.
What is the use of 'animation-iteration-count' in CSS animations?
View Answer:
Here's an example illustrating the 'animation-iteration-count' property:
@keyframes example {
0% {background-color: red;}
100% {background-color: blue;}
}
div {
width: 100px;
height: 100px;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 3;
}
In this example, the animation on the div
element's background color will repeat 3 times.
How do we specify the speed curve of a CSS animation?
View Answer:
Here's an example illustrating how to specify the speed curve of a CSS animation:
@keyframes example {
0% {background-color: red;}
100% {background-color: blue;}
}
div {
width: 100px;
height: 100px;
animation-name: example;
animation-duration: 4s;
animation-timing-function: ease-in-out;
}
In this example, the animation on the div
element's background color will start slow, speed up, then end slow due to the 'ease-in-out' timing function.
Can you explain the difference between CSS animations and transitions?
View Answer:
What is the purpose of 'animation-direction' property?
View Answer:
Here's an example illustrating the 'animation-direction' property:
@keyframes example {
0% {background-color: red;}
100% {background-color: blue;}
}
div {
width: 100px;
height: 100px;
animation-name: example;
animation-duration: 4s;
animation-direction: reverse;
}
In this example, the animation on the div
element's background color will play in reverse direction, starting with blue and ending with red.
Can you pause a CSS animation? If yes, how?
View Answer:
Here's an example illustrating how to pause a CSS animation:
@keyframes example {
0% {background-color: red;}
100% {background-color: blue;}
}
div {
width: 100px;
height: 100px;
animation-name: example;
animation-duration: 4s;
}
div:hover {
animation-play-state: paused;
}
In this example, hovering over the div
element will pause the animation. The 'animation-play-state' is set to 'paused' on hover.
Why do we use CSS animation in web development?
View Answer:
Can you briefly define the idea around CSS transitions?
View Answer:
<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>
What are the four properties used to describe CSS transitions?
View Answer:
Is it possible to utilize the transition property to animate many CSS properties at the same time?
View Answer:
<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>
Could you perhaps explain how to utilize the CSS transition property?
View Answer:
div {
width: 100px; // <-
height: 100px;
background: red;
transition-property: width; // <-
transition-duration: 2s;
}
div:hover {
width: 300px; // <-
}
What does the CSS transition-duration property set?
View Answer:
You may specify multiple durations; each duration gets applied to the related property specified by the transition-property property, which acts as a master list. If fewer durations get specified than in the master list, the user agent repeats the list of durations. If there are more durations in the list, the list truncates to the correct size. In both cases, the CSS declaration stays valid.
div {
width: 100px;
height: 100px;
background: red;
transition-property: width;
transition-duration: 2s; // <-
}
div:hover {
width: 300px;
}
What does the CSS transition-delay property do?
View Answer:
div {
width: 100px;
height: 100px;
background: red;
transition-property: width;
transition-duration: 5s;
transition-delay: 2s; // <-
}
div:hover {
width: 300px;
}
What does the CSS transition-timing-function CSS property do?
View Answer:
div {
width: 100px;
height: 50px;
background: red;
color: white;
font-weight: bold;
transition: width 2s;
}
#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;
}
div:hover {
width: 300px;
}
What does the cubic-bezier() CSS function do?
View Answer:
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s;
transition-timing-function: cubic-bezier(0.1, 0.7, 1, 0.1);
}
div:hover {
width: 300px;
}
What makes the built-in curves different from the cubic-bezier() function?
View Answer:
.myImage {
position: relative;
cursor: pointer;
width: 177px;
height: 160px;
left: 100px;
transition: left 5s cubic-bezier(0.5, -1, 0.5, 2); // <-
}
Can you explain the function of the timing function CSS steps() function?
View Answer:
#stripe.animate {
transform: translate(-90%);
transition-property: transform;
transition-duration: 9s;
transition-timing-function: steps(9, start); /* <-- */
}
Can you explain the function of the CSS transitionend event?
View Answer:
const transition = document.querySelector('.transition');
transition.addEventListener('transitionend', () => {
console.log('Transition ended');
});
Can you describe what CSS keyframes are and how we utilize them in web development?
View Answer:
<div class="progress"></div>
<style>
@keyframes go-left-right {
/* give it a name: "go-left-right" */
from {
left: 0px;
} /* animate from left: 0px */
to {
left: calc(100% - 50px);
} /* animate to left: 100%-50px */
}
.progress {
animation: go-left-right 3s infinite alternate;
/* apply the animation "go-left-right" to the element
duration 3 seconds
number of times: infinite
alternate direction every time
*/
position: relative;
border: 2px solid green;
width: 50px;
height: 20px;
background: lime;
}
</style>