自从
transform
利用GPU,您希望将其用于动画,因为它将具有比所有其他属性使用的基于CPU的动画更好的性能和更少的延迟。
在这里我把
transfrom
价值
scaleX()
和
translateX()
,在哪里
斯卡克斯
将设置其宽度和
翻译器
它的水平位置。
作为注释,
转型
从右到左执行它的值,因此如果切换它们的位置,结果将不同
堆栈片段-就像“非工作”一样,当涉及到如何设置动画时,没有溢出。
.test {
display: flex;
box-sizing: border-box;
width: 384px;
border: 1px solid;
height: 48px;
position: relative;
}
.test::before {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
transform: translateX(0%) scaleX(1);
height: 2px;
background: red;
animation: running-progress 2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
}
@keyframes running-progress {
0% {
transform: translateX(0%) scaleX(1);
}
50% {
transform: translateX(12.5%) scaleX(0.75);
}
100% {
transform: translateX(50%) scaleX(0);
}
}
<div class="test"></div>
堆栈代码段-作为您的“工作”版本,使用
转型
.test {
display: flex;
box-sizing: border-box;
width: 384px;
border: 1px solid;
height: 48px;
position: relative;
}
.test::before {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
transform: translateX(-50%) scaleX(0);
height: 2px;
background: red;
animation: running-progress 2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
}
@keyframes running-progress {
0% {
transform: translateX(-50%) scaleX(0);
}
50% {
transform: translateX(12.5%) scaleX(.75);
}
100% {
transform: translateX(50%) scaleX(0);
}
}
<DIV class=“测试”></DIV>
三个注意事项:
-
一
<div />
不是自动结束标记,它需要开始和结束标记
<div></div>
-
宽度
fill-available
价值决定了工作,而且可能是比下面更好的选择
overflow: hidden
(当另一个孩子可能需要溢出时),并且很快将成为一个选项,尽管它仍然是
实验的
不推荐用于生产
-
有时一个人所需要的就是
溢出:隐藏
,此处适用于您的原始“非工作”样本
.test {
display: flex;
box-sizing: border-box;
width: 384px;
border: 1px solid;
height: 48px;
position: relative;
overflow: hidden;
}
.test::before {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 2px;
background: red;
animation: running-progress 2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
}
@keyframes running-progress {
0% {
margin-left: 0px;
margin-right: 100%;
}
50% {
margin-left: 25%;
margin-right: 0%;
}
100% {
margin-left: 100%;
margin-right: 0;
}
}
<DIV class=“测试”></DIV>