css动画介绍 | 青训营笔记

131 阅读2分钟

文章第一句话为“这是我参与「第四届青训营 」笔记创作活动的第14天 在网页中,添加动画效果更是让网页变得有层次且观赏度提高的一种方式,今天我们就来简单聊一下网页中的动画效果

一. *{ margin: 0; padding: 0; }

    .box1{
        width: 800px;
        height: 800px;
        background-color: silver;
        overflow: hidden;
    }

    .box1 div{
        width: 100px;
        height: 100px;
        margin-bottom: 100px;
        margin-left: 10px;
        
    }

    .box2{
        background-color: #bfa;
        
/* 设置box2的动画 */
/* animation-name: 要对当前元素生效的关键帧的名字 */
/* animation-name: test; */
/* animation-duration: 动画的执行时间 */
/* animation-duration: 4s; */
/* 动画的延时 */
/* animation-delay: 2s; */
/* animation-timing-function: ease-in-out; */
/*
animation-iteration-count 动画执行的次数
可选值:
次数
infinite 无限执行
*/
/* animation-iteration-count: 1; */
/*
animation-direction
指定动画运行的方向
可选值:
normal 默认值 从 from 向 to运行 每次都是这样
reverse 从 to 向 from 运行 每次都是这样
alternate 从 from 向 to运行 重复执行动画时反向执行
alternate-reverse 从 to 向 from运行 重复执行动画时反向执行
*/
/* animation-direction: alternate-reverse; */
/*
animation-play-state: 设置动画的执行状态
可选值:
running 默认值 动画执行
paused 动画暂停
*/
        /* animation-play-state: paused; */

        /* 
        animation-fill-mode: 动画的填充模式
            可选值:
                none 默认值 动画执行完毕元素回到原来位置
                forwards 动画执行完毕元素会停止在动画结束的位置
                backwards 动画延时等待时,元素就会处于开始位置
                both 结合了forwards 和 backwards
        */
        /* animation-fill-mode: both; */
        animation: test 2s 2 1s alternate;

        
    }

    .box1:hover div{
        animation-play-state: paused;
    }
/*
动画
动画和过渡类似,都是可以实现一些动态的效果,
不同的是过渡需要在某个属性发生变化时才会触发
动画可以自动触发动态效果
设置动画效果,必须先要设置一个关键帧,关键帧设置了动画执行每一个步骤
*/
@keyframes test {
/* from表示动画的开始位置 也可以使用 0% */
        from{
            margin-left: 0;
            background-color: orange;
        } 
/* to动画的结束位置 也可以使用100%*/
        to{
            background-color: red;
            margin-left: 700px;
        }
    }
      </style>
<div class="box1">
    <div class="box2"></div>
    <!-- <div class="box3"></div> -->
</div>