具有多个手柄的 JQuery UI 滑块:如何阻止手柄交叉?

发布于 2024-09-14 15:27:25 字数 320 浏览 3 评论 0原文

我正在开发一个快速解决方案,它使用具有多个手柄的滑块来定义动态布局的宽度。

我尝试使用 ExtJS3 和最新的 JQuery UI。

在 ExtJS 中,您可以约束句柄,这样它们就不会相互交叉,这是一种实现我需要的 UI 的非常直观的方法,但是有一些原因我宁愿不将 ExtJS 用于海洋中的一个“岛屿” jQuery。

那么,有谁知道一个秘密属性,或者一些限制 JQuery 滑块中多个句柄的代码?

为了清楚起见:如果您有一个带有 2 个手柄的滑块,一个位于 40,一个位于 60;该约束将阻止您将 60 处的手柄拖动到 20 处,而无需先移动 40 处的手柄。

I'm developing a quick solution that uses a Slider with multiple handles to define widths for a dynamic layout.

I've attempted to use both ExtJS3 and the latest JQuery UI.

In ExtJS, you can constrain the handles so the don't cross over each other, and it's quite an intuitive approach to the UI I need, however there are reasons why I would rather not use ExtJS for one 'island' in a sea of JQuery.

So, does anyone know of a secret attribute, or a bit of code that constrains multiple handles in the JQuery slider ?

For clarity: if you have a slider with 2 handles, one at 40 and one at 60; the constraint would stop you dragging the handle at 60 down to 20, without first moving the 40 one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

奶气 2024-09-21 15:27:25

在滑动事件中,您可以通过检查滑块值并返回 true 以允许滑动或返回 false 以阻止滑动来限制手柄移动(有关更多信息,请参阅 jQuery UI 文档)

In the slide event you can constrain the handle movement by checking the slider values and returning true to allow the slide or false to prevent it (see the jQuery UI docs for more information)

献世佛 2024-09-21 15:27:25

Jquery 滑块使用什么代码?查看范围滑块演示,它有两个手柄,并且不可能交叉它们。

What code are you using for the Jquery slider? Looking at the range slider demo, it has two handles and its not possible to cross them.

我们的影子 2024-09-21 15:27:25

正如 @madcapnmckay 指出的,如果您有一个带有两个手柄的滑块,并且选项中的 range: true ,则手柄不能相互拖动。

我遇到了一个问题,它没有正确约束,但事实证明我有一个字符串 'true' 而不是布尔值 true

As @madcapnmckay pointed out, if you have a slider with two handles and the range: true in the options the handles cannot be dragged past each other.

I was having a problem where it wasn't constraining properly but it turned out I had a string 'true' instead of a boolean true

硪扪都還晓 2024-09-21 15:27:25

我来晚了一点,但我想分享我最紧凑但可读的方法来做到这一点。从技术上讲,它与@tototresde 的答案非常相似。

slide: function (e, ui) {
  // Prevent crossing and overlapping of slider handles.
  if (ui.values[(ui.handleIndex - 1)] >= ui.value ||
      ui.values[(ui.handleIndex + 1)] <= ui.value) {
    return false;
  }
}

I'm a bit late to the party here, but I wanted to share my most compact-yet-readable way to do this. Technically it's pretty similar to @tototresde's answer.

slide: function (e, ui) {
  // Prevent crossing and overlapping of slider handles.
  if (ui.values[(ui.handleIndex - 1)] >= ui.value ||
      ui.values[(ui.handleIndex + 1)] <= ui.value) {
    return false;
  }
}
夏九 2024-09-21 15:27:25

检查这个解决方案:

slide : function( event, ui ) {
        //Actual Handle Selected
        var handleIndex = $(ui.handle).index()-1;

        var diffA, diffB;

        //Check with right handles
        if(handleIndex > 0)
            diffA = ui.values[handleIndex] - ui.values[handleIndex-1];

        //Check with left handles
        if(handleIndex < ui.values.length-1)
            diffB = ui.values[handleIndex+1] - ui.values[handleIndex];

        if (diffA <= 0 || diffB <= 0) { /*checks for the values and compares*/
            return false;
        }
   }

Check this solution:

slide : function( event, ui ) {
        //Actual Handle Selected
        var handleIndex = $(ui.handle).index()-1;

        var diffA, diffB;

        //Check with right handles
        if(handleIndex > 0)
            diffA = ui.values[handleIndex] - ui.values[handleIndex-1];

        //Check with left handles
        if(handleIndex < ui.values.length-1)
            diffB = ui.values[handleIndex+1] - ui.values[handleIndex];

        if (diffA <= 0 || diffB <= 0) { /*checks for the values and compares*/
            return false;
        }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文