2017-04-10 9 views
0

さまざまなバナーサイズを視覚的に表示する範囲スライダを作成しました。私は、.mousedownと.mouseupイベントがトリガされたときに、入力範囲のサムが背景色を変更できるようにする関数を作成しようとしています。私は単純なJQueryを使って親指を直接選択する方法がないことを読んだ。私はクラスの作成を読んで、.AddClassメソッドを使うのが一番です。Jqueryで入力[type = range]のサムスタイルを変更する - 動作しない

私はこれを自分自身で試みましたが、なぜ親が色を変えないのか分かりません。

私のコードを見ていただけますか?

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>

答えて

1

は、それはあなたが持っているCSSの問題です。クラスの背景色の定義に私は重要それを設定し、それが

#range-slider { 
    min-height: 100px; 
    background-color: grey; 
} 

.thumb-down { 
    background-color: red !important; 
} 

.thumb-up { 
    background-color: blue !important; 
} 

を実行TypedSourceが、それはCSSの問題だ、言ったように、この作業フィドル https://jsfiddle.net/w363w6ke/1/

+0

私が使用してお勧めしません!重要 – sm1215

+0

'$( "#範囲スライダ")。( 'マウスダウン'、機能上の(){$全く他の方法はありませんしない限り、 ( '#range-slider')。css( 'background-color'、 'red');}); 'あなたのためのオプションですか? – TypedSource

+0

インラインスタイルを使用せずにこれを行うこともできます – sm1215

3

の顔をしています。以前の背景色を上書きするスタイルを得るためには、セレクタでより具体的にする必要があります。

あなたはこのようにそれを行うことができます。

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

これを試してみて、それは私にとってはうまくいかなかった。 –

+0

これまでのところ、私のコードを変更することなく、これが最良の解決策です。 :) // 1。ユーザーがRange-Thumbでマウスをクリックすると、親指の背景色が青に変わります。$(this).removeClass()。addClass( "thumb-down"); $( "#range-slider")。 .removeClass {}を追加しています。それをリセットすることができます。 https://codepen.io/stinkytofu3311/pen/LWomev – StinkyTofu

+0

また、両方のクラスが同じ要素に同時に存在しないようにjsトグルを変更する必要があります。これのためにremoveClassを使用する必要があります – sm1215

2

だけ使用:青に変更するには、アクティブ擬似セレクタを。マウスイベントは必要ありません。

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>

+0

これは、レンジスライダの背景色を変更するだけだと思います。私はあなたがクリックしたピースを「スライダ - サム」に向けてドラッグし、スライダの値をドラッグして選択します。 – StinkyTofu

+1

ああ、私は誤解しました。使用目的が押されたときにハンドルの色を変更するだけであれば、マウスイベントは必要ありません。単純に:active疑似セレクタを使用することができます。私はこれを反映するために上記のコードを変更しました。追加のクラスとは対照的に、コードはずっと少なく、CSSの1行だけです。 –

+0

ありがとう!これは..より簡単だろうか? 入力[type = range] .thumb-down :: - webkit-slider-thumb { 背景:青; } 入力[type = range] .thumb-up :: - webkit-slider-thumb { 背景:緑; } – StinkyTofu

関連する問題