【问题标题】:How can I make a jquery ui slider with two handles that "push" eachother?如何制作一个带有两个相互“推动”的句柄的 jquery ui 滑块?
【发布时间】:2012-07-20 18:33:51
【问题描述】:

我想使用 jquery ui 制作一个带有两个句柄的滑块,它们之间保持最小的空间。如果左把手试图穿过右把手,则应推动右把手。反之亦然。这是一个例子(不要介意重叠,这并不重要):

http://jsfiddle.net/SP5VQ/

只要移动缓慢,它就可以工作,但如果鼠标移动得太快,它会在“推”场景中失败。我认为在“滑块”事件中设置滑块值可能有问题。

【问题讨论】:

  • 我已经在 Chrome、Firefox 和 IE8 中测试了您的 jsFiddle,在握住手柄后移动鼠标过快时没有问题。请使用不同的鼠标尝试您的 jsFiddle,因为您使用的鼠标左键单击按钮可能有缺陷(移动过程中的短暂中断就是产生您所描述的问题的全部原因)。此外,请删除答案中的“没关系”部分,因为这与要求手柄之间的最小空间相矛盾。话虽如此,您可能想修改您的问题以询问如何在每个句柄之间留出最小空间。干杯!

标签: javascript jquery jquery-ui jquery-ui-slider


【解决方案1】:

这行得通。我从$.ui.slider.prototype._slide 复制了代码,并删除了检查左手柄是否大于右手柄的部分。现在运行良好。

http://jsfiddle.net/Nk8ap/

        $.ui.slider.prototype._slide = function ( event, index, newVal ) {
          var otherVal,
            newValues,
            allowed;

          if ( this.options.values && this.options.values.length ) {
            otherVal = this.values( index ? 0 : 1 );
            if ( newVal !== this.values( index ) ) {
              newValues = this.values();
              newValues[ index ] = newVal;
              // A slide can be canceled by returning false from the slide callback
              allowed = this._trigger( "slide", event, {
                handle: this.handles[ index ],
                value: newVal,
                values: newValues
              } );
              otherVal = this.values( index ? 0 : 1 );
              if ( allowed !== false ) {
                this.values( index, newVal, true );
              }
            }
          } else {
            if ( newVal !== this.value() ) {
              // A slide can be canceled by returning false from the slide callback
              allowed = this._trigger( "slide", event, {
                handle: this.handles[ index ],
                value: newVal
              } );
              if ( allowed !== false ) {
                this.value( newVal );
              }
            }
          }
        }

【讨论】:

  • 我认为这比 UI 滑块的默认行为要好得多。干得好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多