Slider plugin for jQuery
Minimal configuration jQuery plugin with bootstrap style data api.
3kb gzipped and minified.
Add @import "slider.less";
to your bootstrap.less
file.
Compile as normal.
The slider can only be called on input elements of type="text"
, this helps keeping
the api size to minimum.
$('input').slider(options)
Number min
- the minimum value, default1
Number max
- the maximum value, default100
Number step
- the step size, default1
. Must be greater than0
Integer decimals
- the decimal precision for the input element. Must be between0
and8
. By default it is inferred fromstep
. E.g. a step size of0.05
gives decimal precision of2
while0.5
gives1
.Boolean focusable
- is slider focusable? default istrue
Boolean rtl
- specify right-to-left direction on the slider. The default is left-to-right. Only has an effect on horizontally oriented sliders. Automatically picked up from the active CSS writing direction on the element
String decimalPoint
- the decimal point character. Defaults to"."
Integer sensitivity
- scroll wheel sensitivity. Defaults to4
Global options cannot be configured on per-instance basis. This simplifies per-instance configuration for features that only make sense to be equal for all sliders on the page.
E.g. to change decimal point to a comma, write:
$.fn.slider.options.decimalPoint = ",";
Note that the above should be written right after including the script src,
or at latest before DOMReady
event.
.slider("destroy") Destroy the slider enhancement from an input element
$("#price").slider("destroy");
You can also just trigger the event "destroy"
on the target element
$("#price").trigger("destroy")
This way you don't have to remember which plugins to destroy when you remove the element. You need to destroy the plugin when you remove the original input element, otherwise memory will be leaked.
Deprecated .slider("disabled", true|false)
Sets the disabled state of the slider as well as the input element
This method has been deprecated, simply use .prop
on the original input
element
$("#price").prop("disabled", true);
- slidestart - fired before dragging the slider has started. Call
preventDefault()
on the event object to prevent sliding Deprecated* slide - fired constantly as the slider is being dragged - input - fired constantly as the slider is being dragged or otherwise changes value through user action
- slideend - fired before dragging the slider ends
You can use the slider plugin without extra javascript by specifying data attributes on the input element:
<div id="target"></div>
<input data-slider="#target" data-min="1" data-max="100" data-step="1">
The element only needs a data-slider
attribute for it to be picked up. The value is used as a jQuery selector to find the element where the slider will be rendered.
You may also leave selector out, in this case the slider element will be placed before the input element like so:
<div class="slider">
<!-- Slider will be placed here -->
<input data-slider>
</div>
Note: dynamically created elements need to be called manually with js. You may also call $.fn.slider.refresh()
at any point to instantiate any
uninitialized data-slider
inputs. It is automatically called once on DOM ready event. The plugin is also automatically refreshed
after any AJAX request completes.
The disabled attribute of an input is automatically used to disable a slider. A slider will automatically be vertically oriented if its dimensions suggest so (height > width).
See demo.html for better overview and tips for more advanced use.
Building requires Closure Compiler to be placed
one directory up from the project in closure_compiler
directory.
The setting is in Gruntfile.js
, expressed as closurePath: '../closure_compiler'
Clone or download the repository, and while in the project root, run:
npm install
grunt
Builds will appear in the /js
folder. The source code cannot be ran directly without building.