拖动UI输入范围滑块的范围

Rud*_*ger 23 javascript jquery user-input range

<---------[]=====================================[]-------->

0         10                                     90       100
Run Code Online (Sandbox Code Playgroud)

我需要一个带有两个手柄的输入范围滑块来选择一个范围,以及拖动范围的能力(上图中的等号).因此,在上面的示例中,start = 10和end = 90,并通过移动两个句柄之间的整行来向左拖动:

<[]=====================================[]------------------>

0                                       80                 100
Run Code Online (Sandbox Code Playgroud)

现在Start为0,End为80,无需拖动手柄即可完成.

什么库提供此功能?

谢谢.

Ger*_*ári 48

概观

用于rangeDrag特征的jQuery UI Slider小部件扩展.此功能允许用户一次拖动整个范围,而不必拖动手柄来移动范围.

https://gist.github.com/3758297

(function( $, undefined ) {

$.widget("ui.dragslider", $.ui.slider, {

    options: $.extend({},$.ui.slider.prototype.options,{rangeDrag:false}),

    _create: function() {
      $.ui.slider.prototype._create.apply(this,arguments);
      this._rangeCapture = false;
    },

    _mouseCapture: function( event ) { 
      var o = this.options;

      if ( o.disabled ) return false;

      if(event.target == this.range.get(0) && o.rangeDrag == true && o.range == true) {
        this._rangeCapture = true;
        this._rangeStart = null;
      }
      else {
        this._rangeCapture = false;
      }

      $.ui.slider.prototype._mouseCapture.apply(this,arguments);

      if(this._rangeCapture == true) {  
          this.handles.removeClass("ui-state-active").blur();   
      }

      return true;
    },

    _mouseStop: function( event ) {
      this._rangeStart = null;
      return $.ui.slider.prototype._mouseStop.apply(this,arguments);
    },

    _slide: function( event, index, newVal ) {
      if(!this._rangeCapture) { 
        return $.ui.slider.prototype._slide.apply(this,arguments);
      }

      if(this._rangeStart == null) {
        this._rangeStart = newVal;
      }

      var oldValLeft = this.options.values[0],
          oldValRight = this.options.values[1],
          slideDist = newVal - this._rangeStart,
          newValueLeft = oldValLeft + slideDist,
          newValueRight = oldValRight + slideDist,
          allowed;

      if ( this.options.values && this.options.values.length ) {
        if(newValueRight > this._valueMax() && slideDist > 0) {
          slideDist -= (newValueRight-this._valueMax());
          newValueLeft = oldValLeft + slideDist;
          newValueRight = oldValRight + slideDist;
        }

        if(newValueLeft < this._valueMin()) {
          slideDist += (this._valueMin()-newValueLeft);
          newValueLeft = oldValLeft + slideDist;
          newValueRight = oldValRight + slideDist;
        }

        if ( slideDist != 0 ) {
          newValues = this.values();
          newValues[ 0 ] = newValueLeft;
          newValues[ 1 ] = newValueRight;

          // A slide can be canceled by returning false from the slide callback
          allowed = this._trigger( "slide", event, {
            handle: this.handles[ index ],
            value: slideDist,
            values: newValues
          } );

          if ( allowed !== false ) {
            this.values( 0, newValueLeft, true );
            this.values( 1, newValueRight, true );
          }
          this._rangeStart = newVal;
        }
      }



    },


    /*
    //only for testing purpose
    value: function(input) {
        console.log("this is working!");
        $.ui.slider.prototype.value.apply(this,arguments);
    }
    */
});

})(jQuery);
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/omnosis/Swd9S/

用法

HTML

<script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.slider.custom.js"></script>
...
<div id="slider"></div>
Run Code Online (Sandbox Code Playgroud)

JavaScript的

$(function(){
// Slider
$('#slider').dragslider({
    animate: true,   // Works with animation.
    range: true,     // Must have a range to drag.
    rangeDrag: true, // Enable range dragging.
    values: [30, 70]        
});
});
Run Code Online (Sandbox Code Playgroud)

  • 为好工作+1!我们应该看看在https://github.com/jquery/jquery-ui上发出一个pull请求 - 也许他们会将它添加到将来的版本中(?) (6认同)
  • 优秀的演示. (2认同)
  • 这太棒了,谢谢。其他用户使用两位:对于那些使用require.js的用户,您可以将该扩展名另存为自己的文件和shim,类似于jquery-ui:`'jquery-ui-dragslider':{deps:['jquery-ui'] ,导出:“ $”}`,并确保在DragSlider中将方法调用为“ $('#slider')。dragslider()”而不是“ Slider()”,否则会出现jquery-ui错误关于未初始化的组件。 (2认同)

Lg1*_*102 5

noUiSlider提供此功能。您可以通过设置behaviour选项来使用它。它允许固定和用户可更改的范围。没有对 jQueryUI 的依赖,如果你更喜欢 Zepto 而不是 jQuery:那也行。

在此处输入图片说明

披露:我是插件作者。


Jus*_*ier 3

我建议您看一下jQuery UI Slider