The jQuery UI Slider plugin makes selected elements into sliders. There are various options such as multiple handles and ranges. The handle can be moved with the mouse or the arrow keys.
The slider widget will create handle elements with the class ui-slider-handle
on initialization. You can specify custom handle elements by creating and appending the elements and adding the ui-slider-handle
class before initialization. It will only create the number of handles needed to match the length of value
/values
. For example, if you specify values: [ 1, 5, 18 ]
and create one custom handle, the plugin will create the other two.
Theming
The slider widget uses the jQuery UI CSS framework to style its look and feel. If slider specific styling is needed, the following CSS class names can be used:
-
ui-slider
: The track of the slider control. This element will additionally have a class name ofui-slider-horizontal
orui-slider-vertical
depending on theorientation
of the slider.-
ui-slider-handle
: The slider handles. -
ui-slider-range
: The selected range used when therange
option is set. This element can additionally have a class ofui-slider-range-min
orui-slider-range-max
if therange
option is set to"min"
or"max"
respectively.
-
Dependencies
- UI Core
- Widget Factory
- Mouse Interaction
- This widget requires some functional CSS, otherwise it won't work. If you build a custom theme, use the widget's specific CSS file as a starting point.
false
Multiple types supported:
- Boolean: When set to
true
, the handle will animate with the default duration. - String: The name of a speed, such as
"fast"
or"slow"
. - Number: The duration of the animation, in milliseconds.
Code examples:
Initialize the slider with the animate
option specified:
$( ".selector" ).slider({ animate: "fast" });
Get or set the animate
option, after initialization:
// Getter var animate = $( ".selector" ).slider( "option", "animate" ); // Setter $( ".selector" ).slider( "option", "animate", "fast" );
false
true
.Code examples:
Initialize the slider with the disabled
option specified:
$( ".selector" ).slider({ disabled: true });
Get or set the disabled
option, after initialization:
// Getter var disabled = $( ".selector" ).slider( "option", "disabled" ); // Setter $( ".selector" ).slider( "option", "disabled", true );
100
Code examples:
Initialize the slider with the max
option specified:
$( ".selector" ).slider({ max: 50 });
Get or set the max
option, after initialization:
// Getter var max = $( ".selector" ).slider( "option", "max" ); // Setter $( ".selector" ).slider( "option", "max", 50 );
0
Code examples:
Initialize the slider with the min
option specified:
$( ".selector" ).slider({ min: 10 });
Get or set the min
option, after initialization:
// Getter var min = $( ".selector" ).slider( "option", "min" ); // Setter $( ".selector" ).slider( "option", "min", 10 );
"horizontal"
"horizontal"
, "vertical"
.Code examples:
Initialize the slider with the orientation
option specified:
$( ".selector" ).slider({ orientation: "vertical" });
Get or set the orientation
option, after initialization:
// Getter var orientation = $( ".selector" ).slider( "option", "orientation" ); // Setter $( ".selector" ).slider( "option", "orientation", "vertical" );
false
Multiple types supported:
- Boolean: If set to
true
, the slider will detect if you have two handles and create a styleable range element between these two. - String: Either
"min"
or"max"
. A min range goes from the slider min to one handle. A max range goes from one handle to the slider max.
Code examples:
Initialize the slider with the range
option specified:
$( ".selector" ).slider({ range: true });
Get or set the range
option, after initialization:
// Getter var range = $( ".selector" ).slider( "option", "range" ); // Setter $( ".selector" ).slider( "option", "range", true );
1
Code examples:
Initialize the slider with the step
option specified:
$( ".selector" ).slider({ step: 5 });
Get or set the step
option, after initialization:
// Getter var step = $( ".selector" ).slider( "option", "step" ); // Setter $( ".selector" ).slider( "option", "step", 5 );
0
Code examples:
Initialize the slider with the value
option specified:
$( ".selector" ).slider({ value: 10 });
Get or set the value
option, after initialization:
// Getter var value = $( ".selector" ).slider( "option", "value" ); // Setter $( ".selector" ).slider( "option", "value", 10 );
null
range
option is set to true
, the length of values
should be 2.Code examples:
Initialize the slider with the values
option specified:
$( ".selector" ).slider({ values: [ 10, 25 ] });
Get or set the values
option, after initialization:
// Getter var values = $( ".selector" ).slider( "option", "values" ); // Setter $( ".selector" ).slider( "option", "values", [ 10, 25 ] );
- This method does not accept any arguments.
Invoke the destroy method:
$( ".selector" ).slider( "destroy" );
- This method does not accept any arguments.
Invoke the disable method:
$( ".selector" ).slider( "disable" );
- This method does not accept any arguments.
Invoke the enable method:
$( ".selector" ).slider( "enable" );
Retrieves the slider's instance object. If the element does not have an associated instance, undefined
is returned.
Unlike other widget methods, instance()
is safe to call on any element after the slider plugin has loaded.
- This method does not accept any arguments.
Invoke the instance method:
$( ".selector" ).slider( "instance" );
Gets the value currently associated with the specified optionName
.
Note: For options that have objects as their value, you can get the value of a specific key by using dot notation. For example, "foo.bar"
would get the value of the bar
property on the foo
option.
- optionNameType: StringThe name of the option to get.
Invoke the method:
var isDisabled = $( ".selector" ).slider( "option", "disabled" );
- This signature does not accept any arguments.
Invoke the method:
var options = $( ".selector" ).slider( "option" );
Sets the value of the slider option associated with the specified optionName
.
Note: For options that have objects as their value, you can set the value of just one property by using dot notation for optionName
. For example, "foo.bar"
would update only the bar
property of the foo
option.
- optionNameType: StringThe name of the option to set.
- valueType: ObjectA value to set for the option.
Invoke the method:
$( ".selector" ).slider( "option", "disabled", true );
- optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
$( ".selector" ).slider( "option", { disabled: true } );
- This signature does not accept any arguments.
Invoke the method:
var selection = $( ".selector" ).slider( "value" );
- valueType: NumberThe value to set.
Invoke the method:
$( ".selector" ).slider( "value", 55 );
- This signature does not accept any arguments.
Invoke the method:
var values = $( ".selector" ).slider( "values" );
- indexType: IntegerThe zero-based index of the handle.
Invoke the method:
var value = $( ".selector" ).slider( "values", 0 );
Invoke the method:
$( ".selector" ).slider( "values", 0, 55 );
- valuesType: ArrayThe values to set.
Invoke the method:
$( ".selector" ).slider( "values", [ 55, 105 ] );
jQuery
object containing the slider. - This method does not accept any arguments.
Invoke the widget method:
var widget = $( ".selector" ).slider( "widget" );
slidechange
value
method.Initialize the slider with the change callback specified:
$( ".selector" ).slider({ change: function( event, ui ) {} });
Bind an event listener to the slidechange event:
$( ".selector" ).on( "slidechange", function( event, ui ) {} );
slidecreate
Note: The ui
object is empty but included for consistency with other events.
Initialize the slider with the create callback specified:
$( ".selector" ).slider({ create: function( event, ui ) {} });
Bind an event listener to the slidecreate event:
$( ".selector" ).on( "slidecreate", function( event, ui ) {} );
slide
ui.value
represents the value that the handle will have as a result of the current movement. Canceling the event will prevent the handle from moving and the handle will continue to have its previous value.Initialize the slider with the slide callback specified:
$( ".selector" ).slider({ slide: function( event, ui ) {} });
Bind an event listener to the slide event:
$( ".selector" ).on( "slide", function( event, ui ) {} );
slidestart
Initialize the slider with the start callback specified:
$( ".selector" ).slider({ start: function( event, ui ) {} });
Bind an event listener to the slidestart event:
$( ".selector" ).on( "slidestart", function( event, ui ) {} );
slidestop
Initialize the slider with the stop callback specified:
$( ".selector" ).slider({ stop: function( event, ui ) {} });
Bind an event listener to the slidestop event:
$( ".selector" ).on( "slidestop", function( event, ui ) {} );
A simple jQuery UI Slider.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>slider demo</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <style>#slider { margin: 10px; } </style> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> </head> <body> <div id="slider"></div> <script> $( "#slider" ).slider(); </script> </body> </html>
Please login to continue.