How to configure jQuery UI slider for predefined values?

I have a slider in the jQuery user interface where I have some predefined values ​​that the user can select. However, now it is only from 1-60 and does not use my values. I have the following numbers: 1,3,5,15,30,60

This is my code:

$(document).ready(function(){
    var valMap = [1,3,5,15,30,60];
    $("#resolution-slider").slider({
        min: 1,
        max: 60,
        values: [2],
        slide: function(event, ui) {                        
            $("#resolution").val(ui.values[0]);                
            $(".resolution-preview").html(ui.values[0]);                
        }       
    });
});

How to make a slider bind to my values ​​in valMap?

+5
source share
2 answers

Why not do:

$(document).ready(function(){
    var valMap = [1,3,5,15,30,60];
    $("#resolution-slider").slider({
      max: valMap.length - 1, // Set "max" attribute to array length
      min: 0,
      values: [2],
      slide: function(event, ui) {
        $("#resolution").val(valMap[ui.values[0]]); // Fetch selected value from array               
        $(".resolution-preview").html(valMap[ui.values[0]]);
      }  
    });
});
+9
source

I think you want to try the codes below:

<div id="slide"></div>

var valMap = [1,3,5,15,30,60];

$("#slide").slider({
  max: 60,
  min: 1,
  stop: function(event, ui) {
     console.log(ui.value);
  },
  slide: function(event, ui) {
    // 1,3,5,15,30,60
    return $.inArray(ui.value, valMap) != -1;
  }  
});
+2
source

All Articles