简体   繁体   English

Jquery ui滑块移动到下一个值

[英]Jquery ui slider move to the next value

Here's a sample image of what i'm trying to accomplish 这是我想要完成的示例图像

在此输入图像描述

Say I click from 150 value the handle should only move to 50 and not jump to 150. I click again it move from 50 to 100. 假设我从150值单击,手柄应该只移动到50而不会跳到150.我再次点击它从50移动到100。

The other way around, say the handle is on value 150 then i click on value 5, it should not jump directly to 5 it should move from 150 to 100. 反过来说,手柄是值150然后我点击值5,它不应该直接跳到5它应该从150移动到100。

How can I do that? 我怎样才能做到这一点?

Here's my code: 这是我的代码:

var valMap = [5, 10, 20, 50, 100, 150];
value = 0;
var slider = $("#slider").slider({
    disabled: false,
    animate: true,
    min: 0,
    max: valMap.length - 1,
    slide: function (event, ui) {
        slider.slider("option", "animate", "slow");

    },
    change: function () {

        var Slideval = $(this).slider('value');
        console.log(Slideval, value)
        if(value < Slideval) {
            console.log("incremented")
        } else if(value > Slideval) {
            console.log("decremented")
        }
        value = Slideval;
    }
});

Here's a sample jsfiddle: http://jsfiddle.net/endl3ss/jNwww/21/ 这是一个示例jsfiddle: http//jsfiddle.net/endl3ss/jNwww/21/

Then you can use event which will raise when we move slider and then set the value for slider.We can manually set the slider value. 然后你可以使用当我们移动滑块时会产生的事件,然后设置slider的值。我们可以手动设置滑块值。

This is event code 这是事件代码

slide: function( event, ui ) {
            // code
        },




$('#id').slider("value",value); //using this we can set the slider value

I don't know the slider too much, but here is a working solution based on your specification: 我不太了解滑块,但这是一个基于您的规范的工作解决方案:

http://jsfiddle.net/5xMMz/11/ http://jsfiddle.net/5xMMz/11/

and here is the full code for reference 这是完整的参考代码

    var valMap = [5, 10, 20, 50, 100, 150,500,1000,5000];
    value= 0;
    var lastSlideValue = 0;
    var internalSlideCalling = false;

    var slider = $("#slider").slider({
    disabled : false,
    animate : true,
    min : 0,
    max : valMap.length - 1,
    slide : function(event, ui) {
        slider.slider("option", "animate", "slow");
    },
    change: function(){

        var Slideval = $(this).slider('value');

        if (!internalSlideCalling)
        {
            if (Slideval>lastSlideValue && lastSlideValue < $(this).slider('option','max'))
            {
                Slideval = lastSlideValue+1;
            }
            else if (lastSlideValue > $(this).slider('option','min'))
            {
                Slideval = lastSlideValue-1;                
            }
            lastSlideValue = Slideval;
        }

        console.log(Slideval, value)

        if(value < Slideval) {
          console.log("incremented")
        } else if(value > Slideval){
          console.log("decremented")
        }
        value = Slideval;

        if (!internalSlideCalling){
            internalSlideCalling = true;
            $(this).slider('value',Slideval);
        }
        else 
            internalSlideCalling = false;
    }
});

$("#slider").slider('values',valMap);

Use 采用

$("#slider").slider({
    step: 50
})

Here is the link for more information - http://jqueryui.com/slider/#steps 以下是更多信息的链接 - http://jqueryui.com/slider/#steps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM