jquery - 具有任意捕捉点的范围 slider

标签 jquery jquery-ui slider jquery-ui-slider

我已经实现了jQuery range slider在我正在构建的网页上,但似乎无法使其捕捉到特定的非均匀分布值。有谁知道 jQuery slider 是否可以实现这一点,或者是否有我可以使用的替代方案?

举一个更具体的例子,假设我的范围是 0 到 100,并且我有一个像 [0, 7, 32, 61, 67, 90, 100] 这样的数组。我希望范围 slider 只能选择数组中的值。

最佳答案

您可以使用slide callback限制允许的位置:

Triggered on every mouse move during slide. The value provided in the event as ui.value represents the value that the handle will have as a result of the current movement. Canceling the event will prevent the handle from moving and the handle will continue to have its previous value.

所以你只需要这样的东西:

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

allowed.indexOf(ui.value)为负数时,您还可以使用allowed数组并return false;我认为使用对象读起来更好。

演示:http://jsfiddle.net/ambiguous/eRtBa/

关于jquery - 具有任意捕捉点的范围 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8729009/

相关文章:

slider - 基础6 slider : range stating from negative values

javascript - 如何让 jQuery Mobile 站点上的完整站点按钮正常工作?

javascript - 如何通过javascript从父窗口访问子窗口?

javascript - jQuery draggable 在放下时移动

jQuery UI 可排序 : is it possible to restrict possible positions of individual elements?

javascript - 需要 jQuery 内容 slider

javascript - 具有多个条件的 jQuery Slick Slider 过滤

javascript - 如何在没有 cookie 的情况下保存输入值

javascript - 为什么要使用发布/订阅模式(在 JS/jQuery 中)?

javascript - 如何仅在 jquery ui 中的可拖动元素中放置可放置元素