Accessible Dual-Thumb Range Slider for Web & Mobile – DoubleRange
It can be used to help you implement professional-grade range sliders without external dependencies while maintaining accessibility standards.
setFrom(), setTo(), update(), and destroy() for complete control.beforeFromChange and beforeToChange callbacks for validation.1. Install & download.
# NPM $ npm install @ej-kon/double-range
2. Load the double-range’s JavaScript and CSS files in the document.
<link rel="stylesheet" href="double-range.min.css"> <script src="double-range.min.js"></script>
3. Create a slider container element and initialize it with JavaScript:
<div id="priceSlider"></div>
const formatterFn = (v) => `$ ${v}`;
const callbackFn = (from,to) => {console.log(`example1 from:${from} to:${to}`)};
const slider = DoubleRange.create({
selector: "#priceSlider",
min: 30,
max: 500,
from: 100,
to: 400,
step: 1,
label: 'Example 1',
formatter: formatterFn,
delay: 300,
callback: callbackFn
}); 4. If your markup is rendered server-side or you prefer an HTML-first approach, you can initialize the slider on an existing structure using the new DoubleRange() constructor.
<div id="priceSlider">
<div class="double-range">
<div role="group" aria-label="Double Range">
<aside class="start point"></aside>
<aside class="min limit">0</aside>
<label for="doubleRange1-from" class="from"><span>20</span></label>
<div class="track" role="none">
<div class="thumb from"
id="doubleRange1-from" role="slider" tabindex="0"
aria-orientation="horizontal"
aria-valuemin="0" aria-valuemax="100" aria-valuenow="20" aria-valuetext="$20">
</div>
<div class="thumb to"
id="doubleRange1-to" role="slider" tabindex="0"
aria-orientation="horizontal"
aria-valuemin="0" aria-valuemax="100" aria-valuenow="80" aria-valuetext="$80">
</div>
<div class="range-bar"></div>
</div>
<label for="doubleRange1-to" class="to"><span>80</span></label>
<aside class="max limit">100</aside>
<aside class="end point"></aside>
</div>
</div>
</div> 5. All configuration options.
selector (string): CSS selector for the container element.min (number): The minimum possible value of the range.max (number): The maximum possible value of the range.from (number): The initial selected value for the lower thumb.to (number): The initial selected value for the upper thumb.step (number): The increment value for each movement of the thumbs.label (string, optional): An ARIA label for the slider group, improving accessibility.formatter (function, optional): A function to format the displayed values. It receives the raw value and should return a string.callback (function, optional): A function that is called after a value changes. It receives from and to as arguments.delay (number, optional): A debounce delay in milliseconds for the callback function to prevent it from firing too frequently.beforeFromChange (function, optional): A hook function that runs before the from value changes. Must return true to allow the change or false to prevent it.beforeToChange (function, optional): A hook function that runs before the to value changes. Must return true to allow the change or false to prevent it.const slider = DoubleRange.create({
selector: "#priceSlider",
min: 30,
max: 500,
from: 100,
to: 400,
step: 1,
label: 'Example 1',
formatter: formatterFn,
delay: 300,
callback: callbackFn
}); 6. The returned slider instance has several useful methods for programmatic control:
setFrom(value, fireCallback?): Sets the lower thumb’s value. fireCallback (boolean, default true) determines if the callback should be triggered.setTo(value, fireCallback?): Sets the upper thumb’s value. fireCallback (boolean, default true) determines if the callback should be triggered.getFrom(): Returns the current numeric value of the lower thumb.getTo(): Returns the current numeric value of the upper thumb.setMin(min, fireCallback?): Updates the minimum bound of the entire range.setMax(max, fireCallback?): Updates the maximum bound of the entire range.getMin(): Returns the current minimum bound of the range.getMax(): Returns the current maximum bound of the range.update(config, fireCallback?): Bulk updates multiple slider properties at once (min, max, from, to).getRange(): Returns an object with the current range: { from: number, to: number }.destroy(): Removes all event listeners and observers to prevent memory leaks.7. The slider dispatches a custom range-change event on its container element whenever the values change. This is useful for integrating with other parts of your application declaratively.
document.getElementById('priceSlider').addEventListener('range-change', (e) => {
const { from, to } = e.detail;
console.log('Range changed via event:', from, to);
}); Q: Can I prevent users from setting invalid ranges programmatically?
A: Yes, use the beforeFromChange and beforeToChange hooks. These functions receive the proposed new value and must return true to allow the change or false to prevent it. This works for both user interactions and programmatic updates.
Q: How do I handle the slider in responsive layouts?
A: The slider automatically adapts to its container width. We recommend wrapping it in a flexible container and using CSS media queries to adjust thumb sizes and spacing for smaller screens. The touch targets remain accessible even on mobile devices.
Q: How does the library handle decimal step values?
A: Decimal steps work correctly, but be aware of JavaScript floating-point precision issues. For financial applications, consider using integer values (cents instead of dollars) and formatting them in your display logic.
Q: Is there a way to snap thumbs to specific values?
A: The step parameter handles basic snapping. For custom snap points, use the before-change hooks to round values to your preferred snap positions before allowing the change.
The post Accessible Dual-Thumb Range Slider for Web & Mobile – DoubleRange appeared first on CSS Script.
Matt Murdock – aka Daredevil! – is back for Season 2 of Daredevil: Born Again…
HADLEY — A 75,000-square-foot cap on the size of retail businesses, put in place 20…
AMHERST — Representatives from the union for Amherst Department of Public Works employees say their…
The post Photos: A sweet haul appeared first on Daily Hampshire Gazette.
rangeSlider is a pure Vanilla JavaScript library that converts regular Html5 range inputs into responsive,…
Just another pure JS smooth scroll library to animate the page scrolling to specified anchor…
This website uses cookies.