1.0.0 • Published 1 year ago

@swissup/range-slider v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

RangeSlider

Tiny html range slider with two thumbs. 1.5kB of minified and gzipped JS and CSS.

View Demo

Installation

Download source files and add them to the page:

<head>
    <script src="range-slider.js" defer></script>
    <link href="range-slider.css" rel="stylesheet">
</head>

Usage

Add the following html to use a range slider:

<range-slider name="price" min="0" max="100" value="10-90" step="10"/>

Of course, you can work with the component in javascript:

const range = document.querySelector('range-slider');

range.value = [30, 40];

document.addEventListener('range:input', function (event) {
    console.log(event.target.value);
});

Prevent CLS

To prevent layout shift when slider is initializing use the following html instead of short one:

<range-slider name="price" min="0" max="100" value="10-90" step="10">
    <input class="range" type="range" min="0" max="100" value="10" step="10"/>
    <input class="range" type="range" min="0" max="100" value="90" step="10"/>
    <input class="filler" disabled type="range"/>
</range-slider>

Styles

You can style the component with CSS and CSS variables.

VariableDefault Value
--thumb-width16px
--thumb-heightvar(--thumb-width)
--thumb-mobile-scale1.4
--thumb-mobile-widthcalc(var(--thumb-width) * var(--thumb-mobile-scale))
--thumb-mobile-heightcalc(var(--thumb-height) * var(--thumb-mobile-scale))
--thumb-border1px solid #fff
--thumb-border-radius999px
--thumb-bg10 89 254
--thumb-mobile-scale1.4
--track-height4px
--track-border-radiusvar(--thumb-border-radius)
--track-bg234 234 234

Check out the Demo Page for more examples.

Credits

The idea belongs to the unknown StackOverflow user.