【问题标题】:how to change the color of "progress[value]::-webkit-progress-value" property?如何更改“progress[value]::-webkit-progress-value”属性的颜色?
【发布时间】:2019-10-18 16:19:22
【问题描述】:

如何改变颜色

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 或 Jqueryenter image description here

&lt;output&gt;标签用于显示“%”。

【问题讨论】:

    标签: javascript jquery html css


    【解决方案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>
    

    【讨论】:

    • 我的问题不同。您在 HTML 进度标记中定义了硬编码值。但是我希望当用户将该进度条拖到 30% 时,颜色应该会改变。
    【解决方案2】:

    我会使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      相关资源
      最近更新 更多