JQuery Slider, how to make “step” size change

后端 未结 12 734
遥遥无期
遥遥无期 2020-11-27 10:59

Is it possible to use the JQuery Slider (range slider / dual slider) to have non-linear (non consistent \"step\" size) values?

I want to horizontal Slider to look li

相关标签:
12条回答
  • 2020-11-27 11:02

    I think I can offer a better solution: The idea is to keep an array with my real values apart from the slider, then to set the step attribute always to 1 and let the slider work with the index of that array and then fetch the real value from my real data array. Hope this example Helps: http://jsfiddle.net/3B3tt/3/

    0 讨论(0)
  • 2020-11-27 11:03

    Here is my simple solution for a custom (non consistent "step" size) dual-slider (you can modify it to a single-slider if the idea becomes clear) my slider is named "slider-euro", the text-area is named amount-euro as you can see in the code. The idea is to have a slider from 0 to 100 and an array ("realvalues") with 101 places. The slider-value is understood as the place in that array. The only thing is that you have to reference the array when you get the slider values. Here is my Example:

        $(function() {
        var realvalues = [0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000, 60000, 65000, 70000, 75000, 80000, 85000, 90000, 95000, 100000, 105000, 110000, 115000, 120000, 125000, 130000, 135000, 140000, 145000, 150000, 155000, 160000, 165000, 170000, 175000, 180000, 185000, 190000, 195000, 200000, 205000, 210000, 215000, 220000, 225000, 230000, 235000, 240000, 245000, 250000, 255000, 260000, 265000, 270000, 275000, 280000, 285000, 290000, 295000, 300000, 310000, 320000, 330000, 340000, 350000, 360000, 370000, 380000, 390000, 400000, 450000, 500000, 550000, 600000, 650000, 700000, 750000, 800000, 850000, 900000, 1000000, 1500000, 2000000];
    
        $( "#slider-euro" ).slider({
        range: true,
        min: 0,
        max: 100,
        step: 1,
        values: [ 25, 50 ],
        slide: function( event, ui ) {
        $( "#amount-euro" ).val( realvalues[ui.values[ 0 ]] + " € - " + realvalues[ui.values[ 1 ]] + " €");
        }
        });
        $( "#amount-euro" ).val( realvalues[$( "#slider-euro" ).slider( "values", 0 )] + " € - " + realvalues[$( "#slider-euro" ).slider( "values", 1 )]+" €" );
        });
    
    0 讨论(0)
  • 2020-11-27 11:09

    Further to @Broshi's jsfiddle above, here is the code for a custom slider with range disabled:

    jQuery:

    var myData = [1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000,2000,3000,4000,5000,10000,20000,30000,50000,100000,200000,500000,1000000,20000000,30000000];
    slider_config = {
            range: false,
            min: 0,
            max: myData.length - 1,
            step: 1,
            slide: function( event, ui ) {
                // Set the real value into the inputs
                console.log(ui);
                $('#value').val( myData[ ui.value ] );
            },
            create: function() {
                $(this).slider('value',0);
            }
        };
    
    $("#slider").slider(slider_config);
    

    HTML:

    <input id="value" />
    <div id="slider"></div>
    
    0 讨论(0)
  • 2020-11-27 11:11

    Based on the above answer and discussion from @Seburdis, and using the names from your example:

    var prices_array = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200, 250, 500, 1000, 1500, 2000, 2500, 5000, 10000];
    
    function imSliding(event,ui)
    {
        //update the amount by fetching the value in the value_array at index ui.value
        $('#amount').val('$' + prices_arr[ui.value] + ' - $' + prices_arr[prices_arr.length - 1]); 
    }
    
    $('#slider-interval').slider({ min:0, max:values_arr.length - 1, slide: imSliding});
    
    0 讨论(0)
  • 2020-11-27 11:12

    HTML:

    <body>
       <p>
          Slider Value: <span id="val"></span><br/>
          Nonlinear Value: <span id="nlVal"></span><br/>
       </p>
       <div id="slider"></div>
    </body>
    

    JS:

        $(function() {
         var valMap = [0, 25,30,50,55,55,60,100];
         $("#slider").slider({
            // min: 0,
             max: valMap.length - 1,
             slide: function(event, ui) {
               $("#val").text(ui.value);
               $("#nlVal").text(valMap[ui.value]);
             }
         });
        });
    
    0 讨论(0)
  • 2020-11-27 11:16

    Just change the values.

    $( "#price-range" ).slider({
            range: true,
            min: 1000,
            max: 300000000,
            /*step:1,*/
            values: [ 1000, 300000000 ],
            slide: function( event, ui ) {
                if(ui.values[0]<=100000 && ui.values[1]<=100000){
                    $("#price-range").slider({step:10000});
                }else if(ui.values[0]<=300000 && ui.values[1]<=300000){
                    $("#price-range").slider({step:25000});
                }else if(ui.values[0]<=1000000 && ui.values[1]<=1000000){
                    $("#price-range").slider({step:50000});
                }else if(ui.values[0]<=2000000 && ui.values[1]<=2000000){
                    $("#price-range").slider({step:100000});
                }else if(ui.values[0]<=5000000 && ui.values[1]<=5000000){
                    $("#price-range").slider({step:250000});
                }else if(ui.values[0]<=10000000 && ui.values[1]<=10000000){
                    $("#price-range").slider({step:500000});
                }else if(ui.values[0]<=20000000 && ui.values[1]<=20000000){
                    $("#price-range").slider({step:1000000});
                }else if(ui.values[0]<=50000000 && ui.values[1]<=50000000){
                    $("#price-range").slider({step:5000000});
                }else if(ui.values[0]<=50000000 && ui.values[1]<=50000000){
                    $("#price-range").slider({step:10000000});
                }else if(ui.values[0]<=200000000 && ui.values[1]<=200000000){
                    $("#price-range").slider({step:25000000});
                }else{
                    $("#price-range").slider({step:100000000});
                }
    
                $("#mins").val( ui.values[0] );
                $("#maxs").val( ui.values[1] );
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题