如何更改“ progress [value] ::-webkit-progress-value”属性的颜色?

时间:2019-06-03 05:55:24

标签: javascript jquery html css

如何更改

的颜色
progress[value]::-webkit-progress-value {
                background-color: #00bdf8;
            }

用户选择的不同值 当用户选择30%时,颜色应为红色,在60%时,颜色应为黄色,然后为绿色​​

<input type="range" max="100" step="1" class="inputseekbar" id="range">
 <progress max="100" id="progressbarcolor"></progress>
 <output for="range" class="output"></output>

什么是JS或Jquery enter image description here

<output>标签用于显示“%”。

2 个答案:

答案 0 :(得分:0)

我将使用CSS变量,并根据进度值使用JS调整值。

这是一个基本示例:

var progress = document.querySelectorAll("progress");
for(var i = 0;i<progress.length;i++) {
   var n = 2 * parseInt(progress[i].getAttribute("value"));
   progress[i].style.setProperty("--c", "rgb("+n+","+n+",20)");
}
progress {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border: none;
}

progress::-webkit-progress-bar {
  background:grey;
}
progress::-webkit-progress-value {  
  background-color: var(--c,red);
}
<progress max="100"  value="20"></progress>
<progress max="100"  value="50"></progress>
<progress max="100"  value="60"></progress>
<progress max="100"  value="100"></progress>

您也可以轻松地进行更改:

var progress = document.querySelector("progress");

document.querySelector("input").addEventListener('input', function (evt) {
   progress.setAttribute('value',this.value);
   progress.style.setProperty("--c", "rgb("+2*this.value+","+2*this.value+",20)");
});
progress {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border: none;
}

progress::-webkit-progress-bar {
  background:grey;
}
progress::-webkit-progress-value {  
  background-color: var(--c,red);
}
<input type="range" max="100" step="1" class="inputseekbar" id="range">
<progress max="100"  value="50"></progress>


相关:

How to update placeholder color using Javascript?

Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery

答案 1 :(得分:-1)

请检查是否有30%的值

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>


progress {
  color: red;
  background: green
}

progress::-webkit-progress-value {
  background: red;
}

progress::-moz-progress-bar {
  background: green;
}

</style>
</head>
<body>
<input type="range" max="100" step="1" class="inputseekbar" id="range">
 <progress max="100" value="30" id="progressbarcolor"></progress>
 <output for="range" class="output"></output>

</body>
</html>