Very often on the sites of online stores, and not only, you can filters with a slider to select a range of values. In one of the projects, I also needed to make such a slider.
The first thing that comes to mind is to find a ready-made one and paste it on your site. Then I ran into a problem. The JqueryUI.slider plugin I found refused to work on some mobile devices. I could not identify the problem (but not really, and wanted to poke around in someone else's code) and decided to make my own "bike".
I decided to do the "bike" in pure JavaScript so as not to mess with libraries. As one friend of mine says: "the less" left "connections, the more stable everything works."
Therefore, the first thing I did was make up such a simple model
<div class="filter"> <div> <span> 1</span> <div><number>10</number><number>50</number></div> <div class="slider"> <div class="block-min" onMouseDown="moveRange(this)" onTouchStart="moveRange(this)"></div> <div class="color-range"></div> <div class="block-max" onMouseDown="moveRange(this)" onTouchStart="moveRange(this)"></div> </div> </div> <div> <span> 2</span> <div><number>0</number><number>5</number></div> <div class="slider"> <div class="block-min" onMouseDown="moveRange(this)" onTouchStart="moveRange(this)"></div> <div class="color-range"></div> <div class="block-max" onMouseDown="moveRange(this)" onTouchStart="moveRange(this)"></div> </div> </div> </div>
On the screen it looks like this

The slider with sliders itself is in a block with the "slider" class in which there are 2 more blocks: the minimum value slider and the maximum range value slider.
They describe two events:
- onMouseDown - triggered when we grab the slider with the mouse;
- onTouchStart - works on mobile devices, when you touch your finger on our slider.
Above the slider is a block with two numbers - this is our maximum and minimum values.
<div><number>0</number><number>5</number></div>
Now let's move on to the function itself, which is called upon click.
function moveRange (elem) {
The getCoords function allows us to get the coordinates and sizes of our elements.
It looks as follows.
function getCoords(elem) { let coords = elem.getBoundingClientRect(); return {
Now we describe the handler functions.
The first function is onMouseMove, which is called when the mouse moves. It will work out only with horizontal movement.
function onMouseMove(e) { e.preventDefault();
And finally, the handler function of the “release button” event or loss of touch point on mobile devices. It deletes all previously added events and leaves the slider at the set value.
function onMouseUp() { document.removeEventListener('mouseup', onMouseUp); document.removeEventListener('mousemove', onMouseMove); document.removeEventListener('touchend', onMouseUp); document.removeEventListener('touchmove', onMouseMove); }
And, of course, CSS styles for our slider
.filter { padding: 30px; width: 500px; } .filter>div { padding-top: 20px; display: -webkit-flex; display: -moz-flex; display: -ms-flex; display: -o-flex; display: flex; -webkit-flex-direction: column; -moz-flex-direction: column; -ms-flex-direction: column; -o-flex-direction: column; flex-direction: column; } .filter>div>div { display: -webkit-flex; display: -moz-flex; display: -ms-flex; display: -o-flex; display: flex; justify-content: space-between; } .filter .slider { margin-top: 10px; position: relative; height: 6px; background: #fff; border: 1px solid #000; } .filter .color-range { position: absolute; background: #a4a4a4; width: 97%; border: none; height: 6px; left: 15px; } .filter .block-min, .block-max { width: 15px; height: 25px; position: absolute; left: 0; top: -11.5px; background: #fff; border: 1px solid #000; border-radius: 4px; z-index: 1; } .filter .block-max{ left: 97%; }
View the working version of this slider here .
All files can be downloaded from GitHub .
Enjoyable use and easy work!