CSS keeps animation in its final state

CSS keeps the animation in its final state

CSS keeps the animation in its final state

In web development, animations are a very common interactive method. CSS animations can create various transformation effects on page elements, such as translation, rotation, and scaling. However, sometimes we want elements to maintain their final state after the animation ends, rather than immediately returning to their initial state. This requires some techniques. This article will detail how to use CSS to maintain the final state of animations.

Why Maintaining the Final State is Necessary

In many cases, we want elements to maintain their final state after an animation ends, rather than immediately returning to their initial state. This provides users with a better visual experience and makes the page flow more smoothly and naturally. For example, in an image carousel animation, we want a smooth transition between the next image, rather than an abrupt switch.


CSS Animation Basics

Before explaining how to maintain an animation’s final state, let’s review the basics of CSS animation.

@keyframes Rule

In CSS, we use the @keyframes rule to define animation keyframes. The specific syntax is as follows:

@keyframes animation-name {
from {
/* Initial state style */
}
to {
/* End state style */
}
} 

Where animation-name is the name of the animation, which can be customized. from indicates the style state at the beginning of the animation, and to indicates the style state at the end of the animation.

Animation Property

To apply the @keyframes rule to an element, we use the animation property. The syntax is as follows:

.element {
animation: animation-name duration timing-function delay iteration-count direction fill-mode;
} 
  • animation-name: Specifies the name of the animation to apply, which is the name defined in the @keyframes rule.
  • duration: Specifies the duration of the animation in seconds.
  • timing-function: Specifies the timing function of the animation, which controls the speed curve of the animation. Common options include ease, linear, and ease-in.
  • delay: The delay in seconds for the animation to execute.
  • iteration-count: The number of times the animation should play. This can be a specific number of times or infinite for an infinite loop.
  • direction: The direction of the animation. This can be normal or alternate.
  • fill-mode: Specifies the styles to apply before and after the animation. Common values ​​include forwards and backwards.

This is the basics of CSS animations. Next, we’ll learn how to maintain an animation’s final state.

Using the forwards Property

To maintain an animation’s final state, use the forwards value. A value of forwards specifies that the style state that the animation applies to is the state after the animation ends.

Example

Here’s a simple example using the forwards attribute to keep the animation in its final state:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>CSS Animation</title> 
<style> 
.box { 
width: 100px; 
height: 100px; 
background-color: lightblue; 
animation: grow 2s forwards; 
} 

@keyframes grow { 
from { 
width: 100px; 
height: 100px; 
} 
to { 
width: 200px; 
height: 200px; 
} 
} 
</style> 
</head> 
<body> 
<div class="box"></div> 
</body> 
</html> 

In this example, we define an animation called grow that causes the .box element to grow from 100×100 to 200×200 over 2 seconds. By applying the forwards value to the animation, we can ensure that the element remains in its final state after the animation completes.

Using the animationend event

In addition to using the forwards attribute, we can also maintain the final state of the animation by listening for the animationend event.

Example

Here is an example of using the animationend event to keep the animation in its final state:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>CSS Animation</title> 
<style> 
.box { 
width: 100px; 
height: 100px; 
background-color: lightblue; 
animation: grow 2s; 
} 

@keyframes grow { 
from { 
width: 100px; 
height: 100px; 
} 
to { 
width: 200px; 
height: 200px; 
} 
} 
</style> 
</head> 
<body> 
<div class="box"></div> 

<script> 
const box = document.querySelector('.box'); 
box.addEventListener('animationend', () => { 
box.style.width = '200px'; 
box.style.height = '200px'; 
}); 
</script> 
</body> 
</html> 

In this example, we still define an animation called grow, which causes the .box element to grow from 100×100 to 200×200 over 2 seconds. By listening to the animationend event and modifying the element’s style after the animation ends, we can achieve the effect of maintaining the final state of the animation.

Summary

Through this introduction, we learned how to use CSS to maintain the final state of an animation. Whether using the forwards property or listening to the animationend event, you can achieve the effect of maintaining the final state of an element after the animation ends. In actual projects, choosing the appropriate method to achieve animation effects based on specific needs can improve user experience and page interactivity.

Leave a Reply

Your email address will not be published. Required fields are marked *