My solution is to patch jQuery UI and redefine the slide behavior. This is my concept:
If one slider handle passes the boundary made up by the other slider handle during sliding: Instead of preventing the user from sliding further (normal jQuery UI behavior), act as if the user grabbed the other one. To put it simply: Swap the slider handles if neccessary.
This is my patch. It doesn't change much and most lines are identical to the original jQuery UI code.
$.widget('ui.slider', $.ui.slider, {
_start: function( event, index ) {
this.swapIndex = false;
return this._super( event, index );
},
_slide: function( event, index, newVal ) {
var otherVal,
newValues,
allowed;
if ( this.options.values && this.options.values.length ) {
if ( this.swapIndex ) {
index = index ? 0 : 1;
}
otherVal = this.values( index ? 0 : 1 );
if ( index === 0 && newVal > otherVal || index === 1 && newVal < otherVal ) {
this.swapIndex = !this.swapIndex;
index = index ? 0 : 1;
}
if ( newVal !== this.values( index ) ) {
newValues = [];
newValues[ index ] = newVal;
newValues[ index ? 0 : 1 ] = otherVal;
// 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
} );
if ( allowed !== false ) {
this.values( index, newVal );
}
}
} 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 );
}
}
}
}
});
Live demo: http://jsfiddle.net/xykwup5a/
I prefer this solution for two reasons:
- The jQuery UI fix in version 1.10.3 seems not to work if your values are negative
- Better user experience: When not using the patch and two handles overlap the user can only move the handle in one direction and has to play around to increase the range in the other direction.