【发布时间】:2021-06-13 21:20:18
【问题描述】:
我正在尝试制作一个带有动画的“进度条”,可以改变进度条的背景颜色。
该条应在 0% 处以红色开始,随着它在元素上前进,它会在 100% 处变为绿色。我已经 100% 工作了(不,颜色不是很好,但这是未来的问题)......
setTimeout(function(){
var bar = document.getElementById("bar");
bar.style.width = "100%";
bar.classList.add("show");
},10);
#progress {
border:1px solid #888;
background-color:#eee;
height:
width:100%;
}
#bar {
height:30px;
background-color:red;
width:0;
transition: all ease 1s;
}
#bar.show {
background-color:lightgreen;
}
<div id="progress"><div id="bar"></div></div>
问题是(例如)在 50% 时,我无法让条形图在红色和绿色之间的 50% 过渡状态“停止”。
有没有办法计算颜色为 50% 或让 CSS 在特定点停止过渡?
你可以看到如果我有一个 50% 的值会发生什么......它仍然一直到绿色但在 50% 宽度处停止,但我希望它在 50% 过渡颜色处停止(即从红色到红色和绿色的中间)...
setTimeout(function(){
var bar = document.getElementById("bar");
bar.style.width = "50%";
bar.classList.add("show");
},10);
#progress {
border:1px solid #888;
background-color:#eee;
height:
width:100%;
}
#bar {
height:30px;
background-color:red;
width:0;
transition: all ease 1s;
}
#bar.show {
background-color:lightgreen;
}
<div id="progress"><div id="bar"></div></div>
其他信息(应0stone0 的要求)在页面加载时将不知道百分比值,但将通过 AJAX 调用提供。
【问题讨论】:
标签: javascript css css-transitions