CSS动画

2020-11-21  本文已影响0人  小铮冲冲冲

在使用过渡达到动画效果时有很大局限性,因此我们可以使用css中的动画属性来满足我们的需求,首先需要创建一个关键帧:

@keyframes name{
 from{}
 to{}
}

from为动画的开始位置,同为动画的结束位置,动画与过渡的区别在于,动画可以自动触发。我们使用animation作为动画属性

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <link rel="stylesheet" type="text/css" href="css/style.css"/>
    </head>
    <body>
        <div class="content">
            <div class="p1"></div>  
            <div class="p2"></div>
            <div class="p3"></div>
            <div class="p4"></div>
        </div>
    </body>
</html>

.content{
    width: 400px;
    height: 400px;
    background-color: #fff;
}
.p1{
    width: 100px;
    height: 100px;
    background-color: antiquewhite;
    margin-left: 0;
    animation-name: name5;
    animation-duration: 2s;
    animation-iteration-count: infinite;
    animation-direction: alternate;
}

.p2{
    width: 100px;
    height: 100px;
    margin-top: 200px;
    background-color: aliceblue;
    animation-name: name2;
    animation-duration: 2s;
    animation-iteration-count: infinite;
    animation-direction: alternate;
}

.p3{
    width: 100px;
    height: 100px;
    background-color: coral;
    margin-left: 300px;
    margin-top: -400px;
    animation-name: name3;
    animation-duration: 2s;
    animation-iteration-count: infinite;
    animation-direction: alternate;
}

.p4{
    width: 100px;
    height: 100px;
    background-color: darkorchid;
    margin:200px 0 0 300px;
    animation-name: name4;
    animation-duration: 2s;
    animation-iteration-count: infinite;
    animation-direction: alternate;

}

@keyframes name5{
    from{
        margin-left: 0;
        background-color: antiquewhite;}
    to{
        margin-left: 300px;
        background-color: coral;
        }
}
@keyframes name2{
    from{
        margin-top: 200px;
        background-color: aliceblue;
    }
    to{
        margin-top: -100px;
        background-color: antiquewhite;
    }
}
@keyframes name3{
    from{
        background-color: coral;
        margin-left: 300px;
        margin-top: -400px;
    }
    to{
        margin-top: 200px;
        background-color: darkorchid;
    }
}
@keyframes name4{
    from{
        background-color: darkorchid;
        margin:200px 0 0 300px;
    }
    to{
        background-color: aliceblue;
        margin: -100px 0 0 0;
    }
}
通过这些属性可以完成四个块元素自动无限来回移动的动画。同样的,我们可以通过雪碧图来实现一个简单的动画,例如小人的跑步。 run.png
.box1{
    width: 112.25px;
    height: 167px;
    background-image: url(../img/run.png);
    margin: 0 auto;
    animation: run 0.01s infinite steps(4); */
}
@keyframes run{
    from{
        background-position:0 0 ;}
    to{
        background-position:-450px 0 ;
    }
} 

通过steps()设置步长,缩短时间来达到小人奔跑的效果。

上一篇 下一篇

猜你喜欢

热点阅读