Rangeslider

Rangeslider Widget

Rangeslider Widgetversion added: 1.3

Description: Creates a rangeslider widget

QuickNavExamples

Events

Range Slider

The rangeslider widget can be considered as a double handle slider. To add a rangeslider widget to your page, use two standard inputs with the type="range" attribute, and put them inside a <div> container. The input values are used to configure the starting position of the handles and the values are populated in the corresponding text inputs (the first one at the beginning of the rangeslider, and the second one at the end). Specify min and max attribute values to set the rangeslider's range. If you want to constrain inputs to specific increments, add the step attribute. Set the value attribute on each input to define their initial value. The framework will parse these attributes to configure the rangeslider widget.

As you drag the rangeslider's handles, the framework will update the native input values (and vice-versa) so they are always in sync; this ensures that the values are submitted with the form.

Set the for attribute of the labels to match the ids of the inputs so they are semantically associated. It's possible to accessibly hide the labels if they're not desired in the page layout, but we require that they are 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 the data-role="none" attribute to the inputs.

In this example, the acceptable range is 0-100.

<div data-role="rangeslider">
  <label for="range-1a">Rangeslider:</label>
  <input name="range-1a" id="range-1a" min="0" max="100" value="0" type="range" />
  <label for="range-1b">Rangeslider:</label>
  <input name="range-1b" id="range-1b" min="0" max="100" value="100" type="range" />
</div>

The default rangeslider with these settings is displayed like this:

Step increment

To force the range 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 0.1 and the maximum value is 10.

In this example, the acceptable range is 0-100.

<div data-role="rangeslider">
  <label for="range-10a">Rangeslider:</label>
  <input name="range-10a" id="range-10a" min="0" max="10" step=".1" value="0" type="range" />
  <label for="range-10b">Rangeslider:</label>
  <input name="range-10b" id="range-10b" min="0" max="10" step=".1" value="10" type="range" />
</div>

This will produce an input that snaps to increments of 0.1. 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

By default, there is a highlight fill on the track between the two slider handles. To remove it, add the data-highlight="false" attribute to the input. The fill uses the active state swatch.

<div data-role="rangeslider" data-highlight="false">
  <label for="range-2a">Rangeslider (default is "true"):</label>
  <input name="range-2a" id="range-2a" min="0" max="100" value="0" type="range" />
  <label for="range-2b">Rangeslider:</label>
  <input name="range-2b" id="range-2b" min="0" max="100" value="100" type="range" />
</div>

Theming the slider

To set the theme swatch for the rangeslider, add a data-theme attribute to the inputs which will apply the theme to the inputs, handles 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.

<div data-role="rangeslider" data-track-theme="a" data-theme="b">
  <label for="range-3a">Rangeslider:</label>
  <input name="range-3a" id="range-3a" min="0" max="100" value="0" type="range" />
  <label for="range-3b">Rangeslider:</label>
  <input name="range-3b" id="range-3b" min="0" max="100" value="100" type="range" />
</div>

This will produce a themed rangeslider:

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.

<div data-role="rangeslider" data-mini="true">
  <label for="range-4a">Rangeslider:</label>
  <input name="range-4a" id="range-4a" min="0" max="100" value="0" type="range" />
  <label for="range-4b">Rangeslider:</label>
  <input name="range-4b" id="range-4b" min="0" max="100" value="100" type="range" />
</div>

This will produce a rangeslider and its corresponding inputs that are not as tall as the standard version. The inputs also have a smaller text size.

Field containers

Optionally wrap the rangeslider markup in a container with class ui-field-contain 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.

Note: The data- attribute data-role="fieldcontain" is deprecated as of jQuery Mobile 1.4.0 and will be removed in 1.5.0. Use the ui-field-contain class instead.

<div class="ui-field-contain">
  <div data-role="rangeslider">
    <label for="range-7a">Rangeslider:</label>
    <input name="range-7a" id="range-7a" min="0" max="100" value="0" type="range" />
    <label for="range-7b">Rangeslider:</label>
    <input name="range-7b" id="range-7b" min="0" max="100" value="100" type="range" />
  </div>
</div>

The rangeslider 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 rangeslider plugin

This plugin will auto initialize on any page that contains a div with the data-role="rangeslider" attribute. However, if needed you can directly call the rangeslider plugin on any selector, just like any jQuery plugin:

$( "div#range-slider" ).rangeslider();

Options

defaults

Type: Boolean
Default: false
Seting this option to true indicates that other widgets options have default values and causes jQuery Mobile's widget autoenhancement code to omit the step where it retrieves option values from data attributes. This can improve startup time.

This option is also exposed as a data attribute: data-defaults="true".

Code examples:

Initialize the rangeslider with the defaults option specified:

$( ".selector" ).rangeslider({
  defaults: true
});

Get or set the defaults option, after initialization:

// Getter
var defaults = $( ".selector" ).rangeslider( "option", "defaults" );
 
// Setter
$( ".selector" ).rangeslider( "option", "defaults", true );

disabled

Type: Boolean
Default: false
Disables the rangeslider if set to true.

This option is also exposed as a data attribute: data-disabled="true".

Code examples:

Initialize the rangeslider with the disabled option specified:

$( ".selector" ).rangeslider({
  disabled: true
});

Get or set the disabled option, after initialization:

// Getter
var disabled = $( ".selector" ).rangeslider( "option", "disabled" );
 
// Setter
$( ".selector" ).rangeslider( "option", "disabled", true );

highlight

Type: Boolean
Default: true
Sets an active state fill on the track between the two rangeslider handles when set to "true".

This option is also exposed as a data attribute: data-highlight="false".

Code examples:

Initialize the rangeslider with the highlight option specified:

$( ".selector" ).rangeslider({
  highlight: false
});

Get or set the highlight option, after initialization:

// Getter
var highlight = $( ".selector" ).rangeslider( "option", "highlight" );
 
// Setter
$( ".selector" ).rangeslider( "option", "highlight", false );

initSelector

Type: Selector
Default: See below

The default initSelector for the rangeslider widget is:

":jqmData(role='rangeslider')"

Note: This option is deprecated in 1.4.0 and will be removed in 1.5.0.
As of jQuery Mobile 1.4.0, the initSelector is no longer a widget option. Instead, it is declared directly on the widget prototype. Thus, you may specify a custom value by handling the mobileinit event and overwriting the initSelector on the prototype:

$( document ).on( "mobileinit", function() {
  $.mobile.rangeslider.prototype.initSelector = "div.custom";
});

Note: Remember to attach the mobileinit handler after you have loaded jQuery, but before you load jQuery Mobile, because the event is triggered as part of jQuery Mobile's loading process.

The value of this option is a jQuery selector string. The framework selects elements based on the value of this option and instantiates rangeslider widgets on each of the resulting list of elements.

(version deprecated: 1.4.0)

mini

Type: Boolean
Default: null (false)
If set to true, this will display a more compact version of the rangeslider that uses less vertical height by applying the ui-mini class to the outermost element of the rangeslider widget.

This option is also exposed as a data attribute: data-mini="true".

theme

Type: String
Default: null, inherited from parent
Sets the color scheme (swatch) for the rangeslider widget. It accepts a single letter from a-z that maps to the swatches included in your theme.

Possible values: swatch letter (a-z).

This option is also exposed as a data attribute: data-theme="b".

Code examples:

Initialize the rangeslider with the theme option specified:

$( ".selector" ).rangeslider({
  theme: "b"
});

Get or set the theme option, after initialization:

// Getter
var theme = $( ".selector" ).rangeslider( "option", "theme" );
 
// Setter
$( ".selector" ).rangeslider( "option", "theme", "b" );

trackTheme

Type: String
Default: null, inherited from parent
Sets the color scheme (swatch) for the slider's track, specifically. It accepts a single letter from a-z that maps to the swatches included in your theme.

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".

Code examples:

Initialize the rangeslider with the trackTheme option specified:

$( ".selector" ).rangeslider({
  trackTheme: "a"
});

Get or set the trackTheme option, after initialization:

// Getter
var trackTheme = $( ".selector" ).rangeslider( "option", "trackTheme" );
 
// Setter
$( ".selector" ).rangeslider( "option", "trackTheme", "a" );

Methods

destroy()Returns: jQuery (plugin only)

Removes the rangeslider functionality completely. This will return the element back to its pre-init state.
  • This method does not accept any arguments.
Code examples:

Invoke the destroy method:

$( ".selector" ).rangeslider( "destroy" );

disable()Returns: jQuery (plugin only)

Disables the rangeslider.
  • This method does not accept any arguments.
Code examples:

Invoke the disable method:

$( ".selector" ).rangeslider( "disable" );

enable()Returns: jQuery (plugin only)

Enables the rangeslider.
  • This method does not accept any arguments.
Code examples:

Invoke the enable method:

$( ".selector" ).rangeslider( "enable" );

option( optionName )Returns: Object

Gets the value currently associated with the specified optionName.
  • optionName
    Type: String
    The name of the option to get.
Code examples:

Invoke the method:

var isDisabled = $( ".selector" ).rangeslider( "option", "disabled" );

option()Returns: PlainObject

Gets an object containing key/value pairs representing the current rangeslider options hash.
  • This signature does not accept any arguments.
Code examples:

Invoke the method:

var options = $( ".selector" ).rangeslider( "option" );

option( optionName, value )Returns: jQuery (plugin only)

Sets the value of the rangeslider option associated with the specified optionName.
  • optionName
    Type: String
    The name of the option to set.
  • value
    Type: Object
    A value to set for the option.
Code examples:

Invoke the method:

$( ".selector" ).rangeslider( "option", "disabled", true );

option( options )Returns: jQuery (plugin only)

Sets one or more options for the rangeslider.
  • options
    Type: Object
    A map of option-value pairs to set.
Code examples:

Invoke the method:

$( ".selector" ).rangeslider( "option", { disabled: true } );

refresh()Returns: jQuery (plugin only)

update the rangeslider.

If you manipulate a rangeslider via JavaScript, you must call the refresh method on it to update the visual styling.

  • This method does not accept any arguments.
Code examples:

Invoke the refresh method:

$( ".selector" ).rangeslider( "refresh" );

Events

create( event, ui )Type: rangeslidercreate

Triggered when the rangeslider is created.

Note: The ui object is empty but included for consistency with other events.

Code examples:

Initialize the rangeslider with the create callback specified:

$( ".selector" ).rangeslider({
  create: function( event, ui ) {}
});

Bind an event listener to the rangeslidercreate event:

$( ".selector" ).on( "rangeslidercreate", function( event, ui ) {} );

normalize( event )Type: rangeslidernormalize

triggered when the input values are normalized (generally happens when you try to drag one handle past the other).

Note: The ui object is empty but included for consistency with other events.

Code examples:

Initialize the rangeslider with the normalize callback specified:

$( ".selector" ).rangeslider({
  normalize: function( event, ui ) {}
});

Bind an event listener to the rangeslidernormalize event:

$( ".selector" ).on( "rangeslidernormalize", function( event, ui ) {} );

Example:

A basic example of a rangeslider.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>rangeslider demo</title>
  <link rel="stylesheet" href="/doc_/-code-jquery-com-mobile-1-4-5-jquery-mobile-1-4-5-min-css.html?lang=en">
  <script src="//code.jquery.com/jquery-1.10.2.min.js" rel="external nofollow" ></script>
  <script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js" rel="external nofollow" ></script>
</head>
<body>
 
<div data-role="page" id="page1">
  <div data-role="header">
    <h1>jQuery Mobile Example</h1>
  </div>
  <div data-role="rangeslider">
    <label for="range-1a">Rangeslider:</label>
    <input name="range-1a" id="range-1a" min="0" max="100" value="0" type="range">
    <label for="range-1b">Rangeslider:</label>
    <input name="range-1b" id="range-1b" min="0" max="100" value="100" type="range">
  </div>
</div>
 
</body>
</html>

Demo:

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jquerymobile.com/rangeslider

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部