JS进度条颜色不可见

时间:2019-04-11 12:30:24

标签: javascript html css

我使用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文件中获得了标准颜色,也许有人会看到错误。因为我浪费了半天才找到它,但我仍然没有。

1 个答案:

答案 0 :(得分:2)

这提供了您要查找的内容。通过遍历.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>