With this plug-in, you can change the default input range, from this:
<input type="range">
to:
<input type="range">
<script>
$("input").rsSliderLens();
</script>
To use a step 25, you can add a step
attribute to the <input>
element or add a parameter to the plug-in constructor:
<input type="range">
<script>
$("input").rsSliderLens({
step: 25
});
</script>
The 0 and 100 labels are cropped! No problem, just add a padding space equal to 10% of the slider width:
<input type="range">
<script>
$("input").rsSliderLens({
step: 25,
paddingStart: 0.1,
paddingEnd: 0.1
});
</script>
By default <input type="range">
has a range between 0 and 100.
For other ranges, you can add min
and max
attributes to the <input>
element, or in the plug-in constructor:
<input type="range">
<script>
$("input").rsSliderLens({
step: 25,
paddingStart: 0.1,
paddingEnd: 0.1,
min: -50,
max: 150
});
</script>
Great, but now the labels overlap each other.
To fix this, you have some options: (click each one for a sample)
<input type="range">
<style>
input {
width: 400px;
}
</style>
<script>
$("input").rsSliderLens({
step: 25,
paddingStart: 0.1,
paddingEnd: 0.1,
min: -50,
max: 150
});
</script>
<input type="range">
<script>
$("input").rsSliderLens({
width: 400,
step: 25,
paddingStart: 0.1,
paddingEnd: 0.1,
min: -50,
max: 150
});
</script>
<input type="range">
<style>
input {
width: 40px;
height: 300px;
}
</style>
<script>
$("input").rsSliderLens({
step: 25,
paddingStart: 0.1,
paddingEnd: 0.1,
min: -50,
max: 150
});
</script>
<input type="range">
<script>
$("input").rsSliderLens({
step: 25,
min: -50,
max: 150,
ruler: {
labels: {
visible: false
}
}
});
</script>
<input type="range">
<script>
$("input").rsSliderLens({
step: 25,
paddingStart: 0.1,
paddingEnd: 0.1,
min: -50,
max: 150,
fixedHandle: true,
ruler: {
size: 4 // 400% of the <input type=range> width
}
});
</script>
Use a range to restrict input even further:
<input type="range">
<script>
$("input").rsSliderLens({
width: 300,
height: 45,
step: 10,
paddingStart: 0.1,
paddingEnd: 0.1,
range: {
type: [20, 60],
draggable: true
}
});
</script>
Use your mouse to drag the range to other location
If you want labels every 20 positions, but with a step
of 1:
<input type="range"> <script> $("input").rsSliderLens({ step: 1, paddingStart: 0.1, paddingEnd: 0.1, ruler: { labels: { values: [0, 20, 40, 60, 80, 100] } } }); </script>
The same slider, but now with a fixed handle style with a min
of 0 and max
of 500:
<input type="range"> <script> var labels = []; for (var i = 0; i <= 500; i+= 20) { labels.push(i); } $("input").rsSliderLens({ step: 1, paddingStart: 0.1, paddingEnd: 0.1, width: 300, max: 500, fixedHandle: true, ruler: { labels: { values: labels } } }); </script>
Using customized events to display current value inside the handle and to style labels:
<input type="range"> <style> #custom + .range, // range outside the handle #custom ~ .handle .range { // range inside the handle background: -moz-linear-gradient(left, red 0%, green 100%); background: -webkit-linear-gradient(left, red 0%, green 100%); background: linear-gradient(to right, red 0%, green 100%); } </style> <script> $("input").rsSliderLens({ step: 1, paddingStart: 0.1, paddingEnd: 0.1, min: -20, max: 20, ruler: { labels: { values: [-20, -10, 0, 10, 20], onCustomLabel: function(event, value) { return value > 0 ? '+' + value : value; }, onCustomAttrs: function(event, value) { if (value < 0) return { style: 'fill:red' }; if (value > 0) return { style: 'fill:green' }; // no style defined for zero, so returns nothing (or undefined) } } }, onCreate: function (event) { // append a new child <label> element to the .handle $(event.currentTarget).nextAll(".handle").append("<label>"); }, onChange: function (event, value) { // use the <label> element created at run-time, to display the current value var $labelElement = $(event.currentTarget).nextAll(".handle").children("label"); $labelElement.text(value < 0 ? value : ((value > 0 ? '+' : '') + value)); } }); </script>
You have the choice of OnChange
or OnFinalChange
(recommended for computationally expensive tasks)
<input type="range">
<script>
$("input").rsSliderLens({
onChange: function (event, value) {
$changeLabel.text(value);
},
onFinalChange: function (event, value) {
$finalChangeLabel.text(value);
}
});
</script>
Did I mention the support for double handle sliders?
<input type="range">
<script>
$("input").rsSliderLens({
value: [10, 25]
});
</script>
You can bind the plug-in to any element, not only to an <input type=range>
:
<span>Hello world</span> <script> $("span").rsSliderLens({ ruler: { visible: false } }); </script>
<span> <img src="./pic1.jpg" width="60" height="40"> <img src="./pic2.jpg" width="60" height="40"> <img src="./pic3.jpg" width="60" height="40"> </span> <script> $("span").rsSliderLens({ ruler: { visible: false } }); </script>
Or with a little more customization
<span tabindex="0">
<img src="./pic1.jpg" width="60" height="40">
<img src="./pic2.jpg" width="60" height="40">
<img src="./pic3.jpg" width="60" height="40">
</span>
<script>
$("span").rsSliderLens({
max: 2,
step: 1,
paddingStart: .15,
paddingEnd: .15,
ruler: { visible: false }
});
</script>
The tabindex makes the slider keyboard focusable
Responsive design. Please, resize your browser window.
This is possible with the use of relative CSS units.
Don't like the dark layout? You can recompile LESS files to generate the layout you like.
<input type="range"> <script> $("span").rsSliderLens(); </script>
You have total flexibility over the style.
Just change the CSS (not the javascript) to give wings to your imagination.