animation-fill-mode

CSS animation-fill-mode Property

The animation-fill-mode property sets a style to the element when the animation is not executing (before it starts, after it ends, or both).

The animation-fill-mode is one of the CSS3 properties.

The animation-fill-mode property is the only property that affects the element before the first @keyframe is played or after the last keyframe is played. It can assume the following values: “forwards” to specify that the element should keep the style values set by the last keyframe (depending on animation-iteration-count and animation-direction properties); “backwards” to specify that the element should get the style values set by the first keyframe (depends on animation-direction) and keep it within animation-delay period; “both” to specify that the animation should follow the rules for both “forwards” and “backwards”, and “none” (default) to specify that no styles will be applied to the element before or after the animation is executed.

When multiple comma-separated values are specified for any animation property, they will be attached to the animations that are defined in animation-name differently.

类目类目
Initial Valuenone
Applies toAll elements, It also applies to ::before and ::after pseudo-elements.
InheritedNo.
AnimatableNo.
VersionCSS3
DOM Syntaxobject.style.animationFillMode = “forwards”;

Syntax

Syntax

animation-fill-mode: none | forwards | backwards | both | initial | inherit;

Example of the animation-fill-mode property with the “forwards” value:

<!DOCTYPE html>
<html>
  <head>
    <style>
      div {
        width: 100px;
        height: 100px;
        background: #1c87c9;
        position: relative;
        -webkit-animation: element 5s;
        /* Safari 4.0 - 8.0 */
        -webkit-animation-fill-mode: forwards;
        /* Safari 4.0 - 8.0 */
        animation: element 5s;
        animation-fill-mode: forwards;
      }
      /* Safari 4.0 - 8.0 */
      @-webkit-keyframes element {
        from {
          top: 0px;
        }
        to {
          top: 200px;
          background-color: blue;
        }
      }
      @keyframes element {
        from {
          top: 0px;
        }
        to {
          top: 200px;
          background-color: #8ebf42;
        }
      }
    </style>
  </head>
  <body>
    <h2>Animation-fill-mode example</h2>
    <div></div>
  </body>
</html>

Example of the animation-fill-mode property with the “backwards” value:

<!DOCTYPE html>
<html>
  <head>
    <style>
      div {
        width: 100px;
        height: 100px;
        background: #1c87c9;
        position: relative;
        -webkit-animation: element 5s;
        /* Safari 4.0 - 8.0 */
        -webkit-animation-fill-mode: backwords;
        /* Safari 4.0 - 8.0 */
        animation: element 5s;
        animation-fill-mode: backwords;
      }
      /* Safari 4.0 - 8.0 */
      @-webkit-keyframes element {
        from {
          top: 0px;
        }
        to {
          top: 200px;
          background-color: blue;
        }
      }
      @keyframes element {
        from {
          top: 0px;
        }
        to {
          top: 200px;
          background-color: #8ebf42;
        }
      }
    </style>
  </head>
  <body>
    <h2>Animation-fill-mode example</h2>
    <div></div>
  </body>
</html>

Values

Values

ValueDescription
noneThis is the default value. Animation will not apply any style to the element before and after it starts.
forwardsSpecifies that the element should keep the style values set by the last keyframe.
backwordsSpecifies that the element should get the style values set by the first keyframe and keep it within animation-delay period.
bothSpecifies that the animation should follow the rules for both forwards and backwards.
initialIt makes the property use its default value.
inheritIt inherits the property from its parents element.


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部