滑动后jquery ui滑块比较值

时间:2013-05-15 03:06:50

标签: javascript jquery

我试图在值上升或下降时获取滑块的值。

$(document).ready(function() {
    var valMap = [1,2,3,4,5,6,7,8,9,10]; 
    var counter = null;
    var slider = $( "#slider" ).slider({
      disabled: false,
      animate: true,
      min: 0,
      max: valMap.length-1,
      values: [0],
      slide: function( event, ui ) {
        $( "#zomlevel" ).val(valMap[ui.values[0]] + "x");
        slider.slider("option", "animate", "slow");
        counter++;
        if(counter increments){
alert("COUNTER INCREMENTS");
}
if(counter decrement){
alert("COUNTER DECREMENTS");
}
      }
    });
});

如果我向右滑动滑块,它应该增加,如果我向左滑动滑块,它应该减少。我怎么能在滑块中做到这一点?

1 个答案:

答案 0 :(得分:1)

尝试类似

的内容
jQuery(function($){

    var valMap = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    var counter = null, value= 0;
    var slider = $("#slider").slider({
        disabled : false,
        animate : true,
        min : 0,
        max : valMap.length - 1,
        slide : function(event, ui) {
            //$("#zomlevel").val(valMap[ui.values[0]] + "x");
            slider.slider("option", "animate", "slow");
            counter++;

        },
        change: function(){
            var val = $(this).slider('value');

            console.log(val, value)
            if(value < val) {
              console.log('incremented')
            } else if(value > val){
              console.log('decremented')
            }
            value = val;
        }
    });


});

演示:Plunker