有一个progressBar 组件。进度数据取自数据库。JS
不推荐使用。
任务是使动画width
从 0 变为当前值(写在 style 属性中)。
每个进度的生成keyframes
- 不太适合。我还想使用一个具有空值的类,当页面加载时,将其删除并使用transition
. 现在我用max-width
. 动画宽度:0->100%,除了动画速度,一切正常。更准确地说,它们是相同的。
实际上问题是:有没有办法做得更好?谢谢大家!
.progress-bar{
width: 500px;
height: 2px;
background: #aaa;
margin: 10px 0;
}
.progress-bar .progress{
background: #f00;
width:100%;
height: 100%;
animation: width100 2s backwards;
}
@keyframes width100{
0% {
width: 0
}
100%{
width: 100%;
}
}
<div class='progress-bar'>
<div class='progress' style='max-width: 75%'></div>
</div>
<div class='progress-bar'>
<div class='progress' style='max-width: 50%'></div>
</div>
<div class='progress-bar'>
<div class='progress' style='max-width: 20%'></div>
</div>
使用 css 变量,您不必更改动画(您甚至可以通过 js 更改元素的 css 变量)
我还决定动画 not
width
, buttransform
,这是用于动画带颜色:
CSS变量变魔术,可以根据CSS规则继承
需要设置不同的动画速度。
这里(为清楚起见,每个人都设置了相同的最大宽度):