Home > Web Front-end > CSS Tutorial > How Can I Make a CSS3 Animation Stay on Its Last Frame?

How Can I Make a CSS3 Animation Stay on Its Last Frame?

Mary-Kate Olsen
Release: 2024-12-30 22:46:13
Original
687 people have browsed it

How Can I Make a CSS3 Animation Stay on Its Last Frame?

Stopping a CSS3 Animation on Its Last Frame

To prevent an animated element from reverting to its original state after the animation completes, you can use the animation-fill-mode property. This property controls the behavior of the element after the animation has ended.

Solution:

To stop the animation on its last frame (100%), specify the value forwards for the animation-fill-mode property. This will instruct the browser to maintain the final state of the animation after it has finished.

Here's the updated CSS code with the forwards value added:

@keyframes colorchange {
  0%   { transform: scale(1.0) rotate(0deg); }
  50%  { transform: rotate(340deg) translate(-300px,0px) }
  100% { transform: scale(0.5) rotate(5deg) translate(1140px,-137px); }
}

.animated-element {
  animation: colorchange 1s infinite;
  animation-fill-mode: forwards;
  /* Optional: remove the element after the animation finishes */
  transition: 0.5s ease opacity;
  animation-end: hide-element;
}

@keyframes hide-element {
  100% {
    opacity: 0;
  }
}
Copy after login

By using animation-fill-mode: forwards, the element will remain in its transformed state after the animation has finished. Additionally, the optional CSS transition and animation-end keyframe can be used to fade out or remove the element once the animation has completed.

The above is the detailed content of How Can I Make a CSS3 Animation Stay on Its Last Frame?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template