1

I have implemented the jQuery range slider on a webpage I am building, but it does not seem to be possible to make it snap to specific non-uniformly distributed values. Does any one know if this is possible with the jQuery slider or if there is an alternate I can use?

As a more concrete example, say I have a range from 0 to 100, and I have an array like [0, 7, 32, 61, 67, 90, 100]. I want the range slider to only be able to select the values in the array.

4

1 に答える 1

4

slideコールバックを使用して、許可される位置を制限できます。

スライド中にマウスを動かすたびにトリガーされます。イベントで提供されるui.value値は、ハンドルが現在の移動の結果として持つ値を表します。イベントをキャンセルすると、ハンドルは移動できなくなり、ハンドルは以前の値を保持し続けます。

したがって、次のようなものが必要です。

var allowed = { 0: true, 7: true, /*...*/ 100: true };
$("#slider").slider({
    //...
    slide: function(event, ui) {
        if(!allowed[ui.value])
            return false;
    }
});

また、配列を forallowedおよびreturn falsewhenallowed.indexOf(ui.value)が負の場合に使用することもできます。オブジェクトを使用すると読みやすくなると思います。

デモ: http://jsfiddle.net/ambiguous/eRtBa/

于 2012-01-04T21:26:30.987 に答える