Detailed explanation of CSS animation animation

Table of contents

1. The steps of animation animation

Step 1: Define the animation

Step Two: Using Animations

Second, the composite properties of animation

3. The split attribute of animation

4. Animation properties



1. The steps of animation animation

Step 1: Define the animation

//changes为定义的动画名称
  //①两种状态的变化
 @keyframes change {
            from {
                width: 200px;
            }
            to {
                width: 600px;
            }
        }

//②多种状态的变化 
//百分比为动画占总时长的占比
  @keyframes changes {
            0% {
                width: 200px;
            }
            50% {
                width: 500px;
                height: 300px;
            }
            100% {
                width: 800px;
                height: 500px;
            }
        } 

Step Two: Using Animations

//给需要调用动画的类名或标签加上animation change为动画名称 1s为动画时长
.box{
    animation:change 1s;
}

Second, the composite properties of animation

  • animation: animation name, animation time, length, speed curve, delay time, number of repetitions, the state when the animation direction is executed
//animation:动画名称 动画时长 速度曲线 延迟时间 重复次数 动画方向 执行完毕时的状态
//liner为匀速
animation:change 1s linear
//step(3) 为分布动画
animation:change 1s step(3)
 // 5s为延迟时间 infinite无限 alternate反方向 
animation: change 1s steps(3) 5s infinite alternate;
//执行完毕时的状态(最开始backwards,最终forwards) 不能设置重复次数 与 动画方向
animation:change 1s forwards

3. The split attribute of animation

4. Animation properties

  • Frame-by-frame animation (used with the sprite image) animation-timing-function: step(N) N means to divide the animation into N parts (the same number of small images as the sprite image)
  • Multiple sets of animation animation: animation 1, animation 2, animation N (separated by commas)
  •  .box {
                width: 140px;
                height: 140px;
                background-image: url(./images/bg.png);
                /* 跟多个动画用逗号隔开 */
                animation: run 1s steps(12) infinite, moves 1s forwards;
            }
            /* 定义动画 */
            
            @keyframes run {
                /* 背景图改位置 从0的位置到图片的最大宽度*/
                from {
                    background-position: 0 0;
                }
                to {
                    /* 往左挪时负的 */
                    background-position: -1680px 0;
                }
            }
            /* 定义盒子移动的动画 */
            
            @keyframes moves {
                /* 如果最初始的动画开始样式可以省略from */
                from {
                    transform: translateX(0);
                }
                to {
                    transform: translateX(800px);
                }
            }

Guess you like

Origin blog.csdn.net/Gik99/article/details/125752887