
css 中如何保持动画后的样式
想要保持动画后的样式,需要修改 html 和 css 代码。
在提供的 html 代码中,存在如下问题:
这个
立即学习“前端免费学习笔记(深入)”;
要解决这个问题,需要在 html 代码中添加一个初始位置:
接下来,在 css 代码中修改动画的结束属性:
.out {
animation: out 1s;
animation-fill-mode: forwards;
}animation-fill-mode: forwards 确保动画在结束时保持结束属性值。
修改后的 css 代码如下:
.box {
width: 100px;
height: 100px;
position: absolute;
}
.box-in {
background-color: blue;
opacity: 0;
}
.box-out {
background-color: red;
opacity: 1;
}
.out {
animation: out 1s;
animation-fill-mode: forwards;
}
.in {
animation: in 1s;
}
@keyframes out {
from {
left: -70px;
}
to {
left: 70px;
opacity: 0;
}
}
@keyframes in {
from {
left: 70px;
}
to {
left: 0px;
opacity: 1;
}
}现在,动画结束后,div 将保留其 ending position。










