CSS3 animation brings rich dynamic effects to web design, making the page more vivid and attractive. However, sometimes we want the animation to remain in its final state when it ends, rather than returning to its initial state. This article will introduce several ways to achieve this effect and ensure that the animation maintains this state at the end of the action.
Method 1: Use animation-fill-mode
attribute
The animation-fill-mode
property is used to control the style of animation before and after playing. By setting this property, you can achieve the effect of maintaining the final state when the animation ends.
none
: Default value, the animation does not apply any styles before and after playing. forwards
: Keep the final state after the animation ends. backwards
: The animation applies initial state before playing. both
: Combining forwards
and backwards
, the animation applies the initial state before playing and maintains the final state after playing..element { animation: myAnimation 2s ease-in-out; animation-fill-mode: forwards; } @keyframes myAnimation { 0% { transform: translateX(0); } 100% { transform: translateX(100px); } }
In this example, the .element
element will remain in transform: translateX(100px)
state after the animation ends.
Method 2: Use animationend
event
By listening to animationend
event through JavaScript, you can manually set the style of the element when the animation ends, thereby maintaining the animation end state.
Example
<div class="element"></div>
.element { width: 100px; height: 100px; background-color: red; animation: myAnimation 2s ease-in-out; } @keyframes myAnimation { 0% { transform: translateX(0); } 100% { transform: translateX(100px); } }
const element = document.querySelector('.element'); element.addEventListener('animationend', () => { element.style.transform = 'translateX(100px)'; });
In this example, when the animation ends, the JavaScript code sets the element's transform
property to translateX(100px)
, thus maintaining the animation's end state.
Method 3: Use the transition
attribute
Although the transition
property is mainly used for transition effects, by using it cleverly, you can also achieve the effect of maintaining the final state when the animation ends.
Example
<div class="element"></div>
.element { width: 100px; height: 100px; background-color: red; transition: transform 2s ease-in-out; } .element.animate { transform: translateX(100px); }
const element = document.querySelector('.element'); element.classList.add('animate');
In this example, by adding the animate
class, the element's transform
property transitions from its initial state to translateX(100px)
and remains in that state after the transition ends.
Keeping the end state of a CSS3 animation unchanged can be achieved in a variety of ways, including using animation-fill-mode
property, listening to animationend
event, and using the transition
property. Each method has its applicable scenarios, and developers can choose the appropriate method according to specific needs.
animation-fill-mode
attribute: suitable for pure CSS animation, by setting forwards
value to maintain the animation end state. animationend
event: suitable for scenes that require JavaScript interaction, and manually set styles by listening to events. transition
attribute: suitable for transition effects, by adding a class to maintain the animation end state.I hope this article can provide you with a clear understanding and practical guidance to help you better apply CSS3 animation in actual development and achieve the required dynamic effects.
This concludes this article on how to keep the end state of CSS3 animation unchanged. For more information about keeping the end state of CSS3 animation unchanged, please search previous articles on downcodes.com or continue to browse the related articles below. I hope you will do so in the future. Support downcodes.com!