开发者

jQuery UI Slider: Range with 3 handles, and configurable colors?

I would like a jQuery UI slider that has five differently colored ranges defined by three handles. (So the first range 开发者_运维问答would be handle 0 - handle 1, and the second range would be handle 1 to handle 2.) Is this possible through configuration, or must I hack it? If I have to modify the source, is there any guidelines for how to go about doing that?

Update: What I'm looking for in terms of ranges is:

| --- color 1 ----- handle1 --------- color 2 ------------- handle2 ------- color3 --------- handle3 -----color 4 ----|

(hopefully that makes sense.)


The range option defined in the jquery ui slider documentation does indeed limit the slider to only 2 handles. You need to create several elements when the slider raises the 'slide' or 'change' event and set the position of these manually, this way you can create a slider with multiple handles and multiple ranges which you can apply custom css to. See fiddle for a working example.

http://jsfiddle.net/AV3G3/3/


You probably want to use a range slider and pass in an array of 3 values to create 3 handles (according to the docs):

The slider widget will create handle elements with the class 'ui-slider-handle' on initialization. You can specify custom handle elements by creating and appending the elements and adding the 'ui-slider-handle' class before init. It will only create the number of handles needed to match the length of value/values. For example, if you specify 'values: [1, 5, 18]' and create one custom handle, the plugin will create the other two.


If you don't insist on jQuery UI, noUiSlider can give ranges different classes and supports multiple handles - just as you ask. This example demonstrates how to color the ranges.


Modified with onclick reset,please find the complete code.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Slider functionality</title>
      <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>

      <!-- Javascript -->
      <style>
      .slide {
    margin-top: 20px;
    margin-left: 20px;
    width: 400px;
    background: red;
}
.slide-back {
    position:absolute;
    height:100%;
}

      </style>
   </head>

   <body>
      <!-- HTML --> 
      <button class="button">submit</button>
      <p>
         <label for = "price">Price range:</label>
         <input type = "text" id = "price" 
            style = "border:0; color:#b9cd6d; font-weight:bold;">
      </p>
      <div id = "slider-3"></div>
   </body>
   <script>

    var doUpdate = function(event, ui) {
        $('#slider-3 .slide-back').remove();
        $($('#slider-3 a').get().reverse()).each(function(i) {
            var bg = '#fff';
            if(i == 1) {
                bg = 'red';
            } else if(i == 2) {
                bg = 'yellow';
            } else if(i == 3) {
                bg = 'green';
            } 

            $('#slider-3').append($('<div></div>').addClass('slide-back').width($(this).offset().left - 5).css('background', bg));

        });
    };
    $(".button").click(function(){
    $('#slider-3').slider({
        slide: doUpdate,
        change: doUpdate,
        min: 0,
        max: 100,
        values: [ 10, 30, 20],
        slide: function( event, ui ) {
                  $( "#price" ).val( ui.values[ 2 ] );
               }
    });
    $( "#price" ).val( $( "#slider-3" ).slider( "values", 0 )
               );
    doUpdate();
       });
    $('#slider-3').slider({
        slide: doUpdate,
        change: doUpdate,
        min: 0,
        max: 100,
        values: [ 10, 30, 20],
        slide: function( event, ui ) {
                  $( "#price" ).val( ui.values[ 2 ] );
               }
    });
    $( "#price" ).val( $( "#slider-3" ).slider( "values", 0 ) );
    doUpdate();


      </script>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜