Changing the color of a jquery ui slider as you slide it

北慕城南 提交于 2019-11-28 00:14:07

ther is a example

http://jsfiddle.net/amkrtchyan/EAaLK/2/

and a jquery code......

          $( "#tasks_time" ).slider({
                range: "min",
                value: 37,
                min: 1,
                max: 700,
                slide: function() {
                    update();
                }
            });

Aram's answer didn't work for me. Also the jsfiddle demo doesn't seem to do what it's supposed to. Here is a more advanced version that does work, and includes multiple colors.

You can fiddle with it and set as many colors and combinations as you'd like:

The HTML:

<p>A Colored jQuery UI Slider</p>
<div id="coloredSlider"></div>

The CSS:

body {
    font-family: Arial, Helvetica, sans-serif;
}
#coloredSlider {
    float: left;
    clear: left;
width: 600px;
    margin: 15px;
}
#coloredSlider .ui-slider-range { 
    background: #ff0000; 
}

#coloredSlider .ui-state-default, .ui-widget-content .ui-state-default {
    background: none;
    background-color: #FFF;
}

The Javascript:

function getTheColor( colorVal ) {
    var theColor = "";
    if ( colorVal < 50 ) {
                myRed = 255;
                myGreen = parseInt( ( ( colorVal * 2 ) * 255 ) / 100 );
          }
      else  {
                myRed = parseInt( ( ( 100 - colorVal ) * 2 ) * 255 / 100 );
                myGreen = 255;
          }
      theColor = "rgb(" + myRed + "," + myGreen + ",0)"; 
    return( theColor ); 
}

function refreshSwatch() {
    var coloredSlider = $( "#coloredSlider" ).slider( "value" ),
    myColor = getTheColor( coloredSlider );

    $( "#coloredSlider .ui-slider-range" ).css( "background-color", myColor );

    $( "#coloredSlider .ui-state-default, .ui-widget-content .ui-state-default" ).css( "background-color", myColor );
}

$(function() {
      $( "#coloredSlider" ).slider({
            orientation: "horizontal",
            range: "min",
            max: 100,
            value: 0,
            slide: refreshSwatch,
            change: refreshSwatch
      });
});

DEMO: http://codepen.io/anon/pen/obWzaQ

Unkown

Try this:

.ui-widget-content { background: purple; }

As a bonus, if you want to change the color of the little drag handle, use this:

.ui-widget-content .ui-state-default { background: chartreuse; }

 $(function() {
   $( "#slider" ).slider({
       min: 0,
       max: 100,
       slide: function( event, ui ) {
               		$("#slider").css('background', 'linear-gradient(90deg, #c75f04 		                                                   '+ui.value+'%, #cccccc 0%)');
                  $( "#price" ).val( "$" + ui.value);
               }
    });
            
});
<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
<script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<div id = "slider"></div><br>
Price : <input id="price">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!