How to change the input slider thumb colour depending on value?

前端 未结 4 1486
小蘑菇
小蘑菇 2021-01-24 11:03

I\'m making a site that includes a range input slider. I would like the slider thumb to change colour according to the value of the slider.

For example, if the value was

4条回答
  •  长发绾君心
    2021-01-24 12:03

    Your post looks similar to this one: .slider::-webkit-slider-thumb needed in javascript

    const range = document.getElementById("rangeInput");
    var style = document.querySelector('[data="test"]');
    
    range.addEventListener("input", () => {
       const slider_value = range.value;
       let thumb_color;
       if (slider_value <= 29) {
           thumb_color = "rgb(255, 0, 0)";
       }
       else if (slider_value >= 30 && slider_value <= 69) {
           thumb_color = "rgb(255, 255, 0)";
       }
       else {
           thumb_color = "rgb(0, 255, 0)";
       }
       style.innerHTML = `.slider::-webkit-slider-thumb { background-color: ${thumb_color} !important; } .slider:-moz-range-thumb {  ${thumb_color} !important; }`;
    });
    .slider {
        width: 60%;
        margin: 50px auto;
        -webkit-appearance: none;
        height: 8px;
        border-radius: 4px;
        margin-bottom: 15px;
        background-color: rgb(200, 200, 200);
    }
    
    .slider::-webkit-slider-thumb {
       -webkit-appearance: none;
        width: 18px;
        height: 18px;
        border-radius: 10px;
        background-color: rgb(255, 120, 0);
        overflow: visible;
        cursor: pointer;
        transition: all 1s ease;
            
    }
    
    .slidecontainer {
        transform: translateY(-10px);
    }
    
    
    

提交回复
热议问题