【问题标题】:how to change the color of track for range slider?如何更改范围滑块的轨道颜色?
【发布时间】:2017-10-10 00:46:51
【问题描述】:

我必须更改范围滑块的轨道颜色,我尝试使用 css 但在 chrome 中它不起作用,最后我找到了使用 jquery(link) 的解决方案,所以我实现了,但我没有得到预期输出。

jquery:

$('.text-size-slider .slider').change(function () {
      var val = ($(this).val() - $(this).attr('min')) / ($(this).attr('max') - $(this).attr('min'));

      $(this).css('background-image',
                  '-webkit-gradient(linear, left top, right top, '
                  + 'color-stop(' + val + ', #94A14E), '
                  + 'color-stop(' + val + ', #C5C5C5)'
                  + ')'
                  );
  });

这是我的笨蛋: https://plnkr.co/edit/yMg8bKSrbam6RmtgdBl4?p=preview

是否可以在不使用 jquery 的情况下使用纯 javascript 或 angularjs,对于 jquery 也无法正常工作,如 plunker 所示。

预期输出:

任何帮助将不胜感激。 请任何人帮助我。我尝试了很多但无法获得它。

【问题讨论】:

    标签: javascript jquery css angularjs


    【解决方案1】:

    替换style.css

    body {
    
      color: red;
      font-family: "HelveticaNeue", "Helvetica Neue", "Helvetica Neue", Helvetica, Arial, sans-serif;
      font-weight: normal;
      margin: 0;
    }
    
    header {
    
      background-color: white;  
      padding: 60px 40px;
    }
    
    
    h1 {
      font-size: 200%;
    }
    
    h3 {
      font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, sans-serif;
      font-weight: normal;
    }
    
    p {
      font-size: 90%;
      font-weight: normal;
    }
    
    article {
      -webkit-column-count: 4;
      column-count: 4;
    }
    
    p {
      margin: 0px;
    }
    
    /**
     * Text Slider Directive - CSS  
     **/
    .text-size-slider {
      line-height: 100%;
      font-size: 14px;
      position: relative;
    }
    
    .text-size-slider .small-letter,.text-size-slider .big-letter
    {
      font-weight: bold;
    }
    
    .text-size-slider .slider {
      -webkit-appearance: none;
      margin: 0px 8px;
    }
    
    .text-size-slider .slider:focus {
      outline: none;
    }
    
    .text-size-slider .slider::-webkit-slider-thumb {
      border: none;
      cursor: pointer;
      -webkit-appearance: none;
      background-color: rgba(192, 35, 74, 1);
      width: 15px;
      height: 15px;
      border-radius: 50%;
      margin-top: -6px;
    }
    .text-size-slider .slider::-moz-range-thumb {
      border: none;
      cursor: pointer;
      -webkit-appearance: none;
      background-color: rgba(192, 35, 74, 1);
      width: 18px;
      height: 18px;
      border-radius: 50%;
      margin-top: -6px;
    }
    .text-size-slider .slider::-ms-thumb {
      border: none;
      cursor: pointer;
      -webkit-appearance: none;
      background-color: rgba(192, 35, 74, 1);
      width: 15px;
      height: 15px;
      border-radius: 50%;
      margin-top: -6px;
    }
    
    .text-size-slider .slider::-webkit-slider-thumb::before {
     content:"YEAH";
     display:block;
     background:red !important;
     height:20px;
     width:20px;
     position:absolute;
     top:-20px;
     left:-10px;
    }
    .text-size-slider .slider::-webkit-slider-thumb::after {
     content:"YEAH";
     display:block;
     background:green !important;
     height:20px;
     width:20px;
     position:absolute;
     top:-20px;
     left:-10px;
    }
    .text-size-slider .slider::-moz-range-thumb::before {
     content:"YEAH";
     display:block;
     background:rgba(192, 35, 74, 1);
     height:20px;
     width:20px;
     position:absolute;
     top:-20px;
     left:-10px;
    }
    
    .pointer {
        vertical-align:top;
      height: 40px;
      width: 40px;
      border-radius:20px 20px  0 20px;
      background-color:rgba(192, 35, 74, 1);
      display:block;
      transform: rotate(45deg);
      position:absolute;
      top: -39px;
      margin-left:14px;
    
      color:black;
    
    }
    
    .pointer span {
      display: inline-block;
      transform: rotate(-45deg);
      margin-left:12px;
      margin-top: 14px;
      color:white;
    
    
    }
    
    .text-size-slider .slider::-webkit-slider-runnable-track {
      width: 100%;
      height: 2px;
      cursor: pointer;
    
      border: 0;
    }
    .text-size-slider .slider::-ms-track {
      width: 100%;
      height: 2px;
      cursor: pointer;
      background: yellow;
      border: 0;
    }
    .text-size-slider .slider::-moz-range-track {
      width: 100%;
      height: 2px;
      cursor: pointer;
       background-color: #ddd;
      border: 0;
    }
    .text-size-slider .slider::-moz-range-progress {
      background-color: rgba(192, 35, 74, 1); 
      height: 2px;
    }
    .text-size-slider .slider::-webkit-progress-value {
      background-color: orange;
    }{
      background-color: rgba(192, 35, 74, 1); 
    }
    @-moz-document url-prefix() { .pointer { top: -40px; } }
    
     .text-size-slider .slider::-ms-fill-lower {
        background: yellow;
        border-radius: 0;
      }
    
      .text-size-slider .slider::-ms-fill-upper {
        background: black;
        border-radius: 0;
      }
     .text-size-slider .slider{
        -webkit-appearance: none;
        -moz-apperance: none;
        /*! border-radius: 6px; */
        height: 0px;
        background-image: -webkit-gradient( linear, left top, right top, color-stop(0.15, #ddd), color-stop(0.15, #ddd) );
    }
    
    .text-size-slider .slider::-webkit-slider-thumb {
        -webkit-appearance: none !important;
        background-color: #E9E9E9;
        border: 1px solid #CECECE;
        height: 15px;
        width: 15px;
    }
    

    【讨论】:

    • 抱歉,拇指前后的轨道需要两种颜色。
    • 可以放图片吗?正是你想要的
    • 我更新了有问题的图像,请您检查一下,这是我所期望的结果。
    【解决方案2】:

    我已经更新了你的指令,你可以签入这个 plunker

    "https://plnkr.co/edit/UPh0guqcXxO6ivSljh8R?p=preview"
    

    【讨论】:

    • 感谢您的帮助,但这里有一些问题,即我在加载时设置了默认值(18),但颜色达不到这个值。您能帮我吗?
    • angularjs可以吗?
    • 我没听明白,我们已经在使用 Angular 来实现这一点。
    • 但是如果,我删除了 jquery 脚本意味着它不工作
    • 哪个 jquery 脚本?
    猜你喜欢
    • 2023-03-18
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2015-03-06
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多