javascript - JS进度条颜色不可见
问题描述
我使用 jQuery 处理进度条。
所以问题是,我定义了标准颜色,我想在我的进度条上显示取决于我收到的值。
我的代码:
var defaultSegmentColors = ['#FF6363', '#FEF567', '#70FE67'];
function move(value, color, barID) {
var elem = document.getElementById(barID);
var width = 0;
var id = setInterval(frame, 10);
function frame() {
if (width >= value) {
clearInterval(id);
} else {
width++;
elem.style.width = width + '%';
elem.innerHTML = width * 1 + '%';
$('.myBar').css("background-color", color);
}
}
}
function refresh(barElem) {
var color = "";
var elements = {
value: parseInt($(barElem).attr('value')),
idBar: $(barElem).attr('value-field')
}
if (elements['value'] <= 50) {
color = defaultSegmentColors[0];
} else if (elements['value'] <= 80) {
color = defaultSegmentColors[1];
} else if (elements['value'] <= 85) {
color = defaultSegmentColors[2];
}
move(elements['value'], color, elements['idBar']);
}
$('[data-type="sfcs-progres-bar"]').each(function() {
refresh(this);
});
#myProgress {
margin: 5px;
margin-left: 10%;
border-radius: 20px;
width: 85%;
background-color: #ddd;
}
.myBar {
width: 10%;
height: 40px;
background-color: #535353;
text-align: center;
line-height: 40px;
color: white;
border-radius: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myProgress">
<div id="myBar0" class="myBar" data-type="sfcs-progres-bar" value="43" value-field="myBar0"></div>
</div>
<br />
<div id="myProgress">
<div id="myBar1" class="myBar" data-type="sfcs-progres-bar" value="80" value-field="myBar1"></div>
</div>
<br />
<div id="myProgress">
<div id="myBar2" class="myBar" data-type="sfcs-progres-bar" value="90" value-field="myBar2"></div>
</div>
如您所见,我具有可以检查elements['value']的功能,最后我从数组defaultSegmentColors返回一些颜色值,其中我有十六进制颜色。
但最终我从 CSS 文件中获得了标准颜色,也许有人会看到错误。因为我浪费了半天时间才找到它,但我还是没有。
解决方案
这提供了您正在寻找的东西。通过循环遍历.myBar
类,它获取最终元素的值并将其分配给所有条形 - 将类更改为barID
值会导致颜色按照您希望的方式出现。
var defaultSegmentColors = ['#FF6363', '#FEF567', '#70FE67'];
function move(value, color, barID) {
var elem = document.getElementById(barID);
var width = 0;
var id = setInterval(frame, 10);
function frame() {
if (width >= value) {
clearInterval(id);
} else {
width++;
elem.style.width = width + '%';
elem.innerHTML = width * 1 + '%';
$('#' + barID).css("background-color", color);
}
}
}
function refresh(barElem) {
var color = "";
var elements = {
value: parseInt($(barElem).attr('value')),
idBar: $(barElem).attr('value-field')
}
if (elements['value'] <= 50) {
color = defaultSegmentColors[0];
} else if (elements['value'] <= 80) {
color = defaultSegmentColors[1];
} else {
color = defaultSegmentColors[2];
}
move(elements['value'], color, elements['idBar']);
}
$('[data-type="sfcs-progres-bar"]').each(function() {
refresh(this);
});
#myProgress {
margin: 5px;
margin-left: 10%;
border-radius: 20px;
width: 85%;
background-color: #ddd;
}
.myBar {
width: 10%;
height: 40px;
background-color: #535353;
text-align: center;
line-height: 40px;
color: white;
border-radius: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myProgress">
<div id="myBar0" class="myBar" data-type="sfcs-progres-bar" value="43" value-field="myBar0"></div>
</div>
<br />
<div id="myProgress">
<div id="myBar1" class="myBar" data-type="sfcs-progres-bar" value="80" value-field="myBar1"></div>
</div>
<br />
<div id="myProgress">
<div id="myBar2" class="myBar" data-type="sfcs-progres-bar" value="90" value-field="myBar2"></div>
</div>
推荐阅读
- postgresql - Debezium Postgres 和 ElasticSearch - 在 ElasticSearch 中存储复杂对象
- amazon-web-services - 传输层捕获的错误异常 [NettyTcpChannel{localAddress=/x:40554, remoteAddress=/y:9300}],关闭连接
- angular - 如果不使用 routerLink,有什么方法可以发送路由参数?
- python - 每次标志更改时增加排名
- excel - 为什么我的代码没有复制 ThisWorkbook 并使用新的增量名称保存它(为了版本历史)?
- google-maps - Flutter Deeplink 到 Google 地图和 Waze
- r - 扩展数据框以将行名与名称列表匹配的最快方法
- javascript - 单击时隐藏按钮,启动计时器,倒计时完成后自动下载
- reactjs - 放置“ScrollToTop”组件的正确位置是什么?
- python - 如何使用python覆盖或删除文件中的一行