Eyal
Eyal

Reputation: 5848

How can I make a jquery ui slider with two handles that "push" eachother?

I want to use jquery ui to make a slider with two handles that maintain a minimum space between them. If the left handle tries to cross the right handle, the right handle should be pushed along. And vice-versa. Here's an example (nevermind the overlapping, that's not important):

http://jsfiddle.net/SP5VQ/

It works so long as the movement is slow but if the mouse moves too fast, it fails in the "pushing" scenario. I think that setting the sliders values from within the 'slider' event might be at fault.

Upvotes: 2

Views: 1420

Answers (1)

Eyal
Eyal

Reputation: 5848

This worked. I copied the code from $.ui.slider.prototype._slide and removed the part that checked if the left handle was greater than the right handle. Works well now.

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 );
              }
            }
          }
        }

Upvotes: 4

Related Questions