常用动画属性:
animation-name: hd; 设置动画名称(必写)
animation-duration: 2s; 设置动画时间(必写)
animation-fill-mode: forwards; 填充模式:停止到最后一帧,保留0帧时的状态
animation-fill-mode: both; 填充模式:停止到最后一帧,不保留0帧时的状态
animation-delay: 2s; 动画延迟2s后执行
animation-iteration-count: infinite; 设置无限循环动画,也可写数字1234等等,表示动画执行1234次
animation-direction: reverse; 动画执行方向,从100%到0%执行。
animation-direction: alternate; 动画执行方向,从0%到100%执行再从100%到0%执行。
animation-timing-function: ease; 动画执行速度,先快后慢
animation-timing-function: ease-in; 动画执行速度,先慢后快
animation-timing-function: ease-out; 动画执行速度,匀速
animation-timing-function: ease-in-out; 动画执行速度,两边慢,中间快
animation-timing-function: linear; 动画执行速度,渐变
animation-timing-function: steps(4,start); 动画执行速度,4步走完。像俄罗斯方块下降速度。从第二步开始
animation-timing-function: steps(4,end); 动画执行速度,4步走完。像俄罗斯方块下降速度。从第一步开始
div:hover{
animation-play-state:paused; 鼠标放上去动画暂停
animation-play-state:running; 鼠标放上去动画开始
}
//组合模式(名称,时间,渐变速度,延迟,循环次数,方向,填充模式)
animation: name duration timing-function delay iteration-count direction fill-mode;
举例荧光字
<div class="word">abc</div>
.word {
animation: animate linear 2000ms infinite;
}
@keyframes animate {
0% {
opacity: 0.3;
}
100% {
opacity: 1;
text-shadow: 0 0 80px Red, 0 0 30px orange, 0 0 6px DarkRed;
}
}
动画延迟属性
.word {
animation-delay: 900ms;
}
干货1:简单的动画
1、先声明关键帧名称:hd。
2、在main元素上添加animation-name: hd;
3、设置动画时间animation-duration: 2s;
三步即可完成简单的渐变动画
注:如果0%和100%不写,则默认是初始状态。
<style>
body {
width: 100vw;
height: 100vh;
background-color: rgb(21, 6, 77);
display: flex;
align-items: center;
justify-content: center;
}
main {
width: 100px;
height: 100px;
background-color: #fff;
/* 设置动画名称(必写) */
animation-name: hd;
/* 设置动画时间(必写) */
animation-duration: 2s;
}
/* 定义关键帧hd */
@keyframes hd {
/* 起始也可以写from */
0% {
background-color: #fff;
}
/* 结束也可以写to */
100% {
background-color: red;
}
/* 如果有共同属性也可以这样写 */
0%,100%{
border:1px solid yellow;
}
}
</style>
<body>
<main></main>
</body>
干货2:多个动画关键帧一起绑定
<style>
body {
width: 100vw;
height: 100vh;
background-color: rgb(21, 6, 77);
display: flex;
align-items: center;
justify-content: center;
}
main {
width: 400px;
height: 400px;
border: 1px solid #ddd;
}
div {
width: 100px;
height: 100px;
background-color: yellow;
/* 多个关键帧以逗号隔开 */
animation-name: hd,bd;
/* 可以分开定义时间,如果不定义,则执行第一个动画时间 */
animation-duration: 2s,1s;
}
/* 定义关键帧hd */
@keyframes hd {
25% {
transform: translateX(300px);
}
50% {
transform: translate(300px, 300px);
}
75% {
transform: translateY(300px);
}
}
@keyframes bd {
25% {
background-color: red;
}
50% {
background-color: yellow;
}
75% {
background-color: blue;
}
}
</style>
案例1
<style>
main {
width: 100vw;
height: 100vh;
background-color: rgb(21, 6, 77);
transform: scale(0);
animation-name: hd;
animation-duration: 2s;
/* 填充模式:停止到最后一帧 */
animation-fill-mode: forwards;
/* 设置无限循环动画 */
animation-iteration-count: infinite;
display: flex;
align-items: center;
justify-content: center;
}
main::after {
content: "www.baidu.com";
font-size: 2em;
color: #fff;
opacity: 0;
animation-name: opacity;
animation-duration: 2s;
/* 动画延迟2s后执行 */
animation-delay: 2s;
animation-fill-mode: forwards;
}
/* 定义关键帧hd */
@keyframes hd {
25% {
transform: scale(0.5);
}
50% {
transform: scale(1);
}
75% {
transform: scale(0.5);
}
100% {
transform: scale(1);
}
}
@keyframes opacity {
to {
opacity: 0.8;
}
}
</style>
案例2:心动动画
<style>
body {
width: 100vw;
height: 100vh;
background-color: #34495e;
display: flex;
align-items: center;
justify-content: center;
}
.heart {
width: 200px;
height: 200px;
background-color: red;
transform: rotate(45deg);
animation: hd;
animation-duration: 1s;
animation-iteration-count: infinite;
}
@keyframes hd {
50% {
transform: rotate(45deg) scale(1.5);
}
}
.heart::before {
content: "";
width: 200px;
height: 200px;
border-radius: 50%;
position: absolute;
background-color: red;
transform: translateX(-100px);
}
.heart::after {
content: "";
width: 200px;
height: 200px;
border-radius: 50%;
position: absolute;
background-color: red;
transform: translateY(-100px);
}
</style>
<body>
<div class="heart"></div>
</body>