Slider Widgetversion added: 1.0
Description: Creates a slider widget
Slider
To add a slider widget to your page, use a standard input with the type="range"
attribute. The input's value is used to configure the starting position of the handle and the value is populated in the text input. Specify min
and max
attribute values to set the slider's range. If you want to constrain input to specific increments, add the step
attribute. Set the value
attribute to define the initial value. The framework will parse these attributes to configure the slider widget.
As you drag the slider's handle, the framework will update the native input's value (and vice-versa) so they are always in sync; this ensures that the value is submitted with the form.
Set the for
attribute of the label
to match the id
of the input
so they are semantically associated. It's possible to accessibly hide the label if it's not desired in the page layout, but we require that it is present in the markup for semantic and accessibility reasons.
The framework will find all input
elements with a type="range"
and automatically enhance them into a slider with an accompanying input without any need to apply a data-role
attribute. To prevent the automatic enhancement of this input into a slider, add data-role="none"
attribute to the input and wrap them in a div
with the data-role="fieldcontain"
attribute to group them.
In this example, the acceptable range is 0-100.
1
2
|
|
The default slider with these settings is displayed like this:
Step increment
To force the slider to snap to a specific increment, add the step
attribute to the input. By default, the step is 1, but in this example, the step is 10 and the maximum value is 500.
In this example, the acceptable range is 0-100.
1
2
|
|
This will produce an input that snaps to increments of 50. If a value is added to the input that isn't valid with the step increment, the value will be reset on blur to the closest step.
Fill highlight
To have a highlight fill on the track up to the slider handle position, add the data-highlight="true"
attribute to the input. The fill uses active state swatch.
1
2
|
|
Mini version
For a more compact version that is useful in toolbars and tight spaces, add the data-mini="true"
attribute to the element to create a mini version.
1
2
|
|
This will produce a slider and its corresponding input that are not as tall as the standard version. The input also has a smaller text size.
Field containers
Optionally wrap the slider markup in a container with the data-role="fieldcontain"
attribute to help visually group it in a longer form. In this example, the step attribute is omitted to allow any whole number value to be selected.
1
2
3
4
|
|
The slider is now displayed like this:
Sliders also respond to key commands. Right Arrow, Up Arrow and Page Up keys increase the value; Left Arrow, Down Arrow and Page Down keys decrease it. To move the slider to its minimum or maximum value, use the Home or End key, respectively.
Calling the slider plugin
This plugin will auto initialize on any page that contains a text input with the type="range"
attribute. However, if needed you can directly call the slider plugin on any selector, just like any jQuery plugin:
1
|
|
Theming the slider
To set the theme swatch for the slider, add a data-theme
attribute to the input
which will apply the theme to both the input, handle and track. The track swatch can be set separately by adding the data-track-theme
attribute to apply the down state version of the selected button swatch.
1
2
3
4
|
|
This will produce a themed slider:
Flip Toggle Switch
A binary "flip" switch is a common UI element on mobile devices that is used for binary on/off or true/false data input. You can either drag the flip handle like a slider or tap one side of the switch.
To create a flip toggle, start with a select
with two options. The first option will be styled as the "off" state switch and the second will be styled as the "on" state so write your options accordingly.
Set the for
attribute of the label
to match the id
of the input
so they are semantically associated. It's possible to accessibly hide the label if it's not desired in the page layout, but we require that it is present in the markup for semantic and accessibility reasons.
1
2
3
4
5
|
|
This will produce a basic flip toggle switch input. The default styles set the width of the switch to 100% of the parent container and stack the label on a separate line.
Longer Labels
The control is proportionally scaled, so to use longer labels one can just add a line of CSS setting the switch to the desired width. For example, given the following markup:
1
2
3
4
5
6
7
|
|
.containing-element .ui-slider-switch { width: 9em }
will produce:
As some default styles hinge on fieldcontains, note that you may have to ensure that custom styles apply to switches within fieldcontains by using .ui-field-contain div.ui-slider-switch { width: […]; }
.
Mini version
For a more compact version that is useful in toolbars and tight spaces, add the data-mini="true"
attribute to the element to create a mini version.
1
2
3
4
5
|
|
This will produce a flip switch that is not as tall as the standard version and has a smaller text size.
Field containers
Optionally wrap the switch markup in a container with the data-role="fieldcontain"
attribute to help visually group it in a longer form. In this example, the step attribute is omitted to allow any whole number value to be selected.
1
2
3
4
5
6
7
|
|
The flip toggle switch is now displayed like this:
Theming the flip switch
Like all form elements, this widget will automatically inherit the theme from its parent container. To choose a specific theme color swatch, specify the data-theme
attribute on the select
and specify a swatch letter.
1
2
3
4
5
6
7
|
|
This results in a switch with the swatch "a" colors for the handle. Note that the lefthand "on" state gets the active state color.
Here is a swatch "e" variation:
It is also possible to theme the track of the flip switch by adding the data-track-theme
attribute and specifying the chosen swatch letter on the select
.
Here's an example of a flip switch with the swatch "a" applied to the track and swatch "c" applied to the handle:
1
2
3
4
5
6
7
|
|
Calling the switch plugin
This plugin will auto initialize on any page that contains a text input with the type="slider"
attribute. However, if needed you can directly call the slider plugin on any selector, just like any jQuery plugin:
1
|
|
Theming the flip switch
Like all form elements, this widget will automatically inherit the theme from its parent container. To choose a specific theme color swatch, specify the data-theme
attribute on the select and specify a swatch letter.
1
2
3
4
5
6
7
|
|
This results in a switch with the swatch "a" colors for the handle. Note that the lefthand "on" state gets the active state color.
It is also possible to theme the track of the flip switch by adding the data-track-theme attribute and specifying the chosen swatch letter on the select.
Here's an example of a flip switch with the swatch "a" applied to the track and swatch "c" applied to the handle:
1
2
3
4
5
6
7
|
|
Options
disabled
false
Initialize the slider with the disabled
option specified:
1
2
3
|
|
Get or set the disabled
option, after initialization:
1
2
3
4
5
|
|
highlight
false
Initialize the slider with the highlight
option specified:
1
2
3
|
|
Get or set the highlight
option, after initialization:
1
2
3
4
5
|
|
initSelector
"input[type='range'], :jqmData(type='range'), :jqmData(role='slider')"
1
2
3
|
|
mini
false
This option is also exposed as a data attribute: data-mini="true"
.
Initialize the slider with the mini
option specified:
1
2
3
|
|
Get or set the mini
option, after initialization:
1
2
3
4
5
|
|
theme
null, inherited from parent
Possible values: swatch letter (a-z).
This option is also exposed as a data attribute: data-theme="a"
.
Initialize the slider with the theme
option specified:
1
2
3
|
|
Get or set the theme
option, after initialization:
1
2
3
4
5
|
|
trackTheme
null, inherited from parent
Possible values: swatch letter (a-z).
This option can be overridden in the markup by assigning a data attribute to the input, e.g. data-track-theme="a"
.
Initialize the slider with the trackTheme
option specified:
1
2
3
|
|
Get or set the trackTheme
option, after initialization:
1
2
3
4
5
|
|
Methods
disable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the disable method:
1
|
|
enable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the enable method:
1
|
|
refresh()Returns: jQuery (plugin only)
If you manipulate a slider via JavaScript, you must call the refresh method on it to update the visual styling.
- This method does not accept any arguments.
Invoke the refresh method:
1
|
|
Events
create( event, ui )Type: slidecreate
Note: The ui
object is empty but included for consistency with other events.
Initialize the slider with the create callback specified:
1
2
3
|
|
Bind an event listener to the slidecreate event:
1
|
|
start( event )Type: slidestart
-
eventType: Event
Note: The ui
object is empty but included for consistency with other events.
Initialize the slider with the start callback specified:
1
2
3
|
|
Bind an event listener to the slidestart event:
1
|
|
stop( event )Type: slidestop
-
eventType: Event
Note: The ui
object is empty but included for consistency with other events.
Initialize the slider with the stop callback specified:
1
2
3
|
|
Bind an event listener to the slidestop event:
1
|
|
Examples:
A basic example of a slider.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
|
Demo:
A basic example of a flip toggle switch.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
|