如何使用标记创建范围滑块并更改填充范围的颜色?

Posted

技术标签:

【中文标题】如何使用标记创建范围滑块并更改填充范围的颜色?【英文标题】:How to create range silder with marker and change color of filled range? 【发布时间】:2019-08-21 13:54:59 【问题描述】:

我已经在某种程度上完成了如下所示的工作。如何实现这样的范围滑块?

.slidecontainer 
  width: 100%; /* Width of the outside container */


/* The slider itself */
.slider 
  -webkit-appearance: none;  /* Override default CSS styles */
  appearance: none;
  width: 100%; /* Full-width */
  height: 25px; /* Specified height */
  background: #d3d3d3; /* Grey background */
  outline: none; /* Remove outline */
  opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
  -webkit-transition: .2s; /* 0.2 seconds transition on hover */
  transition: opacity .2s;


/* Mouse-over effects */
.slider:hover 
  opacity: 1; /* Fully shown on mouse-over */


/* The slider handle (use -webkit- (Chrome, Opera, Safari, Edge) and -moz- (Firefox) to override default look) */ 
.slider::-webkit-slider-thumb 
  -webkit-appearance: none; /* Override default look */
  appearance: none;
  width: 25px; /* Set a specific slider handle width */
  height: 25px; /* Slider handle height */
  background: #4CAF50; /* Green background */
  cursor: pointer; /* Cursor on hover */


.slider::-moz-range-thumb 
  width: 25px; /* Set a specific slider handle width */
  height: 25px; /* Slider handle height */
  background: #4CAF50; /* Green background */
  cursor: pointer; /* Cursor on hover */
<div class="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
</div>

【问题讨论】:

您没有具体说明问题所在。你的代码有错误吗? 我想获得这样的东西:drive.google.com/file/d/1YL12pzvCCaIVLLCdPTPElWs6NNcR6HBl/view 【参考方案1】:

如果您想要问题中的确切输出,请在 Firefox 和 Chrome 上进行测试。

const _R = document.querySelector('[type=range]');
    _R.style.setProperty('--val', +_R.value);
    _R.style.setProperty('--max', +_R.max);
    _R.style.setProperty('--min', +_R.min);

    document.documentElement.classList.add('js');

    _R.addEventListener('input', e => 
        _R.style.setProperty('--val', +_R.value);
    , false);
input:focus
        outline: none;
    
    .slider 
        -webkit-appearance: none;
        --range: calc(var(--max) - var(--min));
        --ratio: calc((var(--val) - var(--min))/var(--range));
        --sx: calc(.5*1.5em + var(--ratio)*(100% - 1.5em));
        margin: 0;
        padding: 0;
        width: 100%;
        height: 1.5em;
        background: transparent;
        font: 1em/1 arial, sans-serif;
        border: none;
    
    .slider, .slider::-webkit-slider-thumb 
        -webkit-appearance: none;
    
    .slider::-webkit-slider-runnable-track 
        box-sizing: border-box;
        border: none;
        width: 12.5em;
        height: 0.5em;
        background: #ccc;
    
    .js .slider::-webkit-slider-runnable-track 
        background: linear-gradient(#7b1c1a, #7b1c1a) 0/var(--sx) 100% no-repeat #ccc;
    
    .slider::-moz-range-track 
        box-sizing: border-box;
        border: none;
        height: 0.5em;
        background: #ccc;
    
    .slider::-ms-track 
        box-sizing: border-box;
        border: none;
        width: 12.5em;
        height: 0.5em;
        background: #ccc;
    
    .slider::-moz-range-progress 
        height: 0.5em;
        background: #7b1c1a;
    
    .slider::-ms-fill-lower 
        height: 0.5em;
        background: #7b1c1a;
    
    .slider::-webkit-slider-thumb 
        margin-top: -0.550em;
        box-sizing: border-box;
        border: none;
        width: 1.5em;
        height: 1.5em;
        border-radius: 50%;
        background: #7b1c1a;
    
    .slider::-moz-range-thumb 
        box-sizing: border-box;
        border: none;
        width: 1.5em;
        height: 1.5em;
        border-radius: 50%;
        background: #7b1c1a;
    
    .slider::-ms-thumb 
        margin-top: 0;
        box-sizing: border-box;
        border: none;
        width: 1.5em;
        height: 1.5em;
        border-radius: 50%;
        background: #7b1c1a;
    
    .slider::-ms-tooltip 
        display: none;
    
    #tickmarks 
        display: flex;
        justify-content: space-between;
        padding: 0 10px;
    

    #tickmarks p 
        position: relative;
        display: flex;
        justify-content: center;
        text-align: center;
        width: 1px;
        background: #D3D3D3;
        height: 10px;
        line-height: 40px;
        margin: 0 0 20px 0;
    
<div class="slidecontainer">
    <input type="range" min="5" max="20" value="10" step='2.5' class="slider" id="myRange" list='tickmarks'>
    <div id="tickmarks">
        <p>5</p>
        <p>7.5</p>
        <p>10</p>
        <p>12.5</p>
        <p>15</p>
        <p>17.5</p>
        <p>20</p>
    </div>
</div>

典型的跨浏览器解决方案,

/*Chrome*/
    @media screen and (-webkit-min-device-pixel-ratio:0) 
        input[type='range'] 
          overflow: hidden;
          width: 100%;
          -webkit-appearance: none;
          background-color: #d3d3d3;
        
        
        input[type='range']::-webkit-slider-runnable-track 
          height: 25px; /* Specified height */
          -webkit-appearance: none;
          color: #13bba4;
          margin-top: -1px;
        
        
        input[type='range']::-webkit-slider-thumb 
           width: 25px; /* Set a specific slider handle width */
          -webkit-appearance: none;
          height: 25px; /* Specified height */
          cursor: ew-resize;
          background: #434343;
          box-shadow: -100vw 0 0 100vw  #4CAF50;
        
    
    
    /** FF*/
    input[type="range"]::-moz-range-progress 
      background-color: #4CAF50; 
    
    input[type="range"]::-moz-range-track   
      background-color: #d3d3d3;
    
    /* IE*/
    input[type="range"]::-ms-fill-lower 
      background-color: #4CAF50; 
    
    input[type="range"]::-ms-fill-upper   
      background-color: #d3d3d3;
        
<div class="slidecontainer">
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
</div>

【讨论】:

以上是关于如何使用标记创建范围滑块并更改填充范围的颜色?的主要内容,如果未能解决你的问题,请参考以下文章

更改范围滑块的颜色

如何使范围滑块的左侧与滑块右侧的颜色不同?

使用范围滑块通过 javascript 更改 HSL 背景颜色

如何标记我的 html 范围滑块?

如何更改滑块范围样式?

Python matplotlib更改超出颜色条范围的值的默认颜色