【问题标题】:Change Input[type=range] thumb styles with Jquery - Not Working使用 Jquery 更改 Input[type=range] 拇指样式 - 不工作
【发布时间】:2017-04-11 06:20:58
【问题描述】:

我创建了一个范围滑块,可以直观地展示不同的横幅尺寸。我正在尝试创建一个函数,该函数允许 input-range-thumb 在触发 .mousedown 和 .mouseup 事件时更改其背景颜色。我读到无法使用简单的 Jquery 直接选择拇指。我读到创建一个类,并使用 .AddClass 方法是最好的。

我自己尝试过,但我似乎无法弄清楚为什么拇指不会改变颜色。

你能看看我的代码吗?

https://codepen.io/stinkytofu3311/pen/LWomev

    //1. When user clicks their mouse down on the Range-Thumb the thumbs background-color will change to blue.
$("#range-slider").on("mousedown", function() {
    $(this).addClass("thumb-down");
});
//2. When user mouse up on the Range-Thumb the Thumbs background-color will change to Green.
$("#range-slider").on("mouseup", function() {
    $(this).addClass("thumb-up");
});

问题解决了!这是固定版本 https://codepen.io/stinkytofu3311/pen/OpKKMd

var imageUrl = new Array(); 

        imageUrl[0] = 'http://svgshare.com/i/1Ak.svg';

        imageUrl[1] = 'http://svgshare.com/i/1AQ.svg';

        imageUrl[2] = 'http://svgshare.com/i/1Bb.svg';

        imageUrl[3] = 'http://svgshare.com/i/1Am.svg';

        imageUrl[4] = 'http://svgshare.com/i/1CG.svg';

        imageUrl[5] = 'http://svgshare.com/i/1By.svg';
       
$(document).on('input change', '#range-slider', function() {//listen to slider changes
    var v=$(this).val();//getting slider val
   $('#sliderStatus').html( $(this).val() );
  $("#img").prop("src", imageUrl[v]);
});


// ::::: Range Slider Thumb ::::: //

//1. When user clicks their mouse down on the Range-Slider
$("#range-slider").on("mousedown", function() {
  //1.1 Remove default class from CSS, and add the class .thumb-down (changes background color)
    $(this).removeClass().addClass("thumb-down");
  //1.2 Remove default class from CSS, and add the class .hover-ring (changes box-shadow to a green color)
    $(this).removeClass().addClass("hover-ring");
});
//2. When user mouse up on the Range-Slider the Thumbs background-color will change to Green
$("#range-slider").on("mouseup", function() {
  //2.1 Add class .thumb-up
    $(this).addClass("thumb-up");
    $(this).addClass("hover-ring-out");
});
.product-range-wrapper {
  displat: -webkit-flex;
  displat:flex;
  -webkit-flex-direction: column;
  flex-direction:column;
  width:600px;
  margin:0px auto;
  /*outline: 1px solid purple;*/
 }
.product-range-block {
  display: -webkit-flex;
  display:flex;
  -webkit-flex-direction: row;
  flex-direction: row;
  width:100%;
  height:300px;
  /*outline: 1px solid red;*/
}
.ref-height-block {
  flex-grow:3;
  /*background-color:red;*/
}
.size-chart-block {
  flex-grow:9;
  /*background-color:green;*/
}
.product-range-block img {
  width:90%;
  /*outline: 1px solid blue;*/
}
#img {
  width: 100% !important;
}
#slider_count {
  margin:0px auto;
  width:200px;
  padding:20px 20px;
  text-align:center;
  background-color:yellow;
}

/* ::::::::::::::::::::Range Slider Styles::::::::::::::::::::::::: */
.range-slider-block {
  margin:0px auto;
  width:90%;
  }
#range-slider {
  padding:40px 0px;
  width:100%;
  /*outline: 1px solid green;*/
}
/* Remove Range Sliders Default Styles*/
input[type=range]{
    -webkit-appearance: none;
}
/* Track */
input[type=range]::-webkit-slider-runnable-track {
    height: 10px;
    background: #d7d7d7;
    border: none;
    border-radius: 6px;
}
input[type=range]:focus {
    outline: none;
}
/* Thumb */
input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    border: none;
    height: 30px;
    width: 30px;
    border-radius: 50%;
    background: #46947F;
    margin-top: -9px;
    transition: box-shadow 0.5s;
}
input[type=range]:hover::-webkit-slider-thumb {
    box-shadow: 0 0 0 6pt rgba(190,190,190,0.4);
    cursor:pointer;
}

/* JS Stykes */
/* Changes Thumb color to darker green when mousedownn */
input[type=range].thumb-down::-webkit-slider-thumb {
  background:#316557;
}
/* Changes Thumb color back to light green when mouseup */
input[type=range].thumb-up::-webkit-slider-thumb {
  background:#46947F;  
}
/* Changes Ring color Green */
input[type=range].hover-ring::-webkit-slider-thumb {
    box-shadow: 0 0 0 6pt rgba(70,148,127,0.46);
    cursor:pointer;
}
input[type=range].hover-ring-out::-webkit-slider-thumb {
    box-shadow: 0 0 0 0pt rgba(0,0,0,0);
    cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product-range-wrapper">
  
  <div class="product-range-block">
    <div class="ref-height-block">
      <img src="http://svgshare.com/i/1Ba.svg" alt="Product Height Refrence" height="" width="">
    </div>
    <div class="size-chart-block">
      <img src="http://svgshare.com/i/1Ak.svg" style='' id='img'/>
    </div>
  </div>
  
  <div class="range-slider-block">
    <input type="range" id="range-slider" value="0.0" min="0" max="5" step="1" />
  </div>
  
  
</div>




<div id="slider_count">slider value = <span id="sliderStatus">0</span></div>

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    就像 TypedSource 所说,这是一个 CSS 问题。您只需在选择器中更加具体,以便让您的样式覆盖之前的背景颜色。

    你可以这样做:

    input[type=range].thumb-down::-webkit-slider-thumb {
      background:blue;
    }
    input[type=range].thumb-up::-webkit-slider-thumb {
      background:green;  
    }
    

    【讨论】:

    • 到目前为止,这是最好的解决方案,无需过多更改我的代码。 :) //1。当用户在 Range-Thumb 上单击鼠标时,拇指背景颜色将变为蓝色。 $("#range-slider").on("mousedown", function() { $(this).removeClass().addClass("thumb-down"); });添加 .removeClass{}。允许它重置。 codepen.io/stinkytofu3311/pen/LWomev
    • 您可能还需要更改您的 js 切换,以便两个类不会同时存在于同一个元素上。需要为此使用 removeClass
    • 谢谢!你知道为什么 imageUrl[0] 在页面加载时默认不显示吗?
    【解决方案2】:

    只需使用 :active 伪选择器更改为蓝色。你甚至不需要鼠标事件。

    var imageUrl = new Array(); 
    
            imageUrl[0] = 'http://svgshare.com/i/1Ak.svg';
    
            imageUrl[1] = 'http://svgshare.com/i/1AQ.svg';
    
            imageUrl[2] = 'http://svgshare.com/i/1Bb.svg';
    
            imageUrl[3] = 'http://svgshare.com/i/1Am.svg';
    
            imageUrl[4] = 'http://svgshare.com/i/1CG.svg';
    
            imageUrl[5] = 'http://svgshare.com/i/1By.svg';
           
    $(document).on('input change', '#range-slider', function() {//listen to slider changes
        var v=$(this).val();//getting slider val
       $('#sliderStatus').html( $(this).val() );
      $("#img").prop("src", imageUrl[v]);
    });
    .product-range-wrapper {
      displat: -webkit-flex;
      displat:flex;
      -webkit-flex-direction: column;
      flex-direction:column;
      width:600px;
      margin:0px auto;
      /*outline: 1px solid purple;*/
     }
    .product-range-block {
      display: -webkit-flex;
      display:flex;
      -webkit-flex-direction: row;
      flex-direction: row;
      width:100%;
      height:300px;
      /*outline: 1px solid red;*/
    }
    .ref-height-block {
      flex-grow:3;
      /*background-color:red;*/
    }
    .size-chart-block {
      flex-grow:9;
      /*background-color:green;*/
    }
    .product-range-block img {
      width:90%;
      /*outline: 1px solid blue;*/
    }
    #img {
      width: 100% !important;
    }
    #slider_count {
      margin:0px auto;
      width:200px;
      padding:20px 20px;
      text-align:center;
      background-color:yellow;
    }
    /*#range-slider {
      -webkit-appearance: none;
        width: calc(100% - (0px));
        height: 10px;
        border-radius: 5px;
        background: #d7dcdf;
        outline: none;
        padding: 0;
        margin: 0;
    }*/
    
    /* ::::::::::::::::::::Range Slider Styles::::::::::::::::::::::::: */
    .range-slider-block {
      margin:0px auto;
      width:90%;
      }
    #range-slider {
      padding:40px 0px;
      width:100%;
      /*outline: 1px solid green;*/
    }
    /* Remove Range Sliders Default Styles*/
    input[type=range]{
        -webkit-appearance: none;
    }
    /* Track */
    input[type=range]::-webkit-slider-runnable-track {
        height: 10px;
        background: #d7d7d7;
        border: none;
        border-radius: 6px;
    }
    input[type=range]:focus {
        outline: none;
    }
    /*input[type=range]:focus::-webkit-slider-runnable-track {
        background: #ccc;
    }*/
    /* Thumb */
    input[type=range]::-webkit-slider-thumb {
        -webkit-appearance: none;
        border: none;
        height: 30px;
        width: 30px;
        border-radius: 50%;
        background: #46947F;
        margin-top: -9px;
        transition: box-shadow 0.5s;
    }
    input[type=range]:hover::-webkit-slider-thumb {
        box-shadow: 0 0 0 4pt #BEBEBE;
    }
    input[type=range]::-webkit-slider-thumb:active {
      background:blue;  
    }
    
    /*input[type=range]:focus::-webkit-slider-thumb {
        background:#ffffff;
    } */
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="product-range-wrapper">
      
      <div class="product-range-block">
        <div class="ref-height-block">
          <img src="http://svgshare.com/i/1Ba.svg" alt="Product Height Refrence" height="" width="">
        </div>
        <div class="size-chart-block">
          <img src="" style='' id='img'/>
        </div>
      </div>
      
      <div class="range-slider-block">
        <input type="range" id="range-slider" value="0.0" min="0" max="5" step="1" />
      </div>
      
      
    </div>
    
    
    
    
    <div id="slider_count">slider value = <span id="sliderStatus">0</span></div>

    【讨论】:

    • 我认为这只是改变了范围滑块轨道背景颜色。我正在尝试定位您单击的“滑块拇指”,然后拖动以选择滑块值。
    • 啊,我误会了。如果您的目标只是在用户按下手柄时更改手柄的颜色,则不需要鼠标事件,您可以简单地使用 :active 伪选择器。我已经更改了上面的代码以反映这一点。与其他类相比,它的代码要少得多,并且在您的 css 中只有 1 行。
    • 谢谢!这会比..容易吗?输入[类型=范围].thumb-down::-webkit-slider-thumb { 背景:蓝色; } 输入[类型=范围].thumb-up::-webkit-slider-thumb { 背景:绿色; }
    • 是的,它更简单,因为您不需要使用 javascript 或 jquery 来添加和删除类。这是一个纯 CSS 解决方案。您的正常状态为绿色...并且您的 :active 状态为蓝色(请参阅此答案中的代码 sn-p )并注意 javascript 不再存在,而我添加的只是新的带有:active 伪选择器的选择器
    • 那是天才。 :) 我即将发布第二个问题,但我想我会问。你知道为什么 image[0] 默认不显示在页面加载上吗?滑块加载空白图像,并且仅在用户移动 range-slider-thumb 时才显示 image[0]。
    【解决方案3】:

    这是您遇到的 CSS 问题。在类背景颜色定义中,我将其设置为重要并运行

    #range-slider {
      min-height: 100px;
      background-color: grey;
    }
    
    .thumb-down {
      background-color: red !important;
    }
    
    .thumb-up {
      background-color: blue !important;
    }
    

    看看这个工作小提琴 https://jsfiddle.net/w363w6ke/1/

    【讨论】:

    • 我不建议使用 !important 除非绝对没有其他方法
    • $("#range-slider").on('mousedown', function(){$('#range-slider').css('background-color', 'red');}); 适合您吗?
    • 你也可以不使用内联样式来做到这一点
    • 非常感谢。如果您对为什么 imageUrl[0] 在页面加载时默认不显示有任何建议,我们将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 2010-11-23
    • 1970-01-01
    • 2016-07-18
    相关资源
    最近更新 更多