Skip to content

Commit 7ea2887

Browse files
committed
add custom range slider
1 parent 5e8bbb2 commit 7ea2887

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

44-custom range slider/index.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<link rel="stylesheet" href="style.css" />
7+
<title>Custom Range Slider</title>
8+
</head>
9+
<body>
10+
<h2>Custom Range Slider</h2>
11+
<div class="range-container">
12+
<input type="range" name="range" id="range" min="0" max="100" />
13+
<label for="range">50</label>
14+
</div>
15+
<script src="script.js"></script>
16+
</body>
17+
</html>

44-custom range slider/script.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const range = document.getElementById("range");
2+
3+
// https://stackoverflow.com/questions/10756313/javascript-jquery-map-a-range-of-numbers-to-another-range-of-numbers
4+
const scale = (num, in_min, in_max, out_min, out_max) => {
5+
return ((num - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min;
6+
};
7+
8+
range.addEventListener("input", (e) => {
9+
const value = +e.target.value;
10+
const label = e.target.nextElementSibling;
11+
const rangeWidth = getComputedStyle(e.target).getPropertyValue("width");
12+
const labelWidth = getComputedStyle(label).getPropertyValue("width");
13+
// remove px
14+
const numWidth = +rangeWidth.substring(0, rangeWidth.length - 2);
15+
const numLabelWidth = +labelWidth.substring(0, labelWidth.length - 2);
16+
const max = +e.target.max;
17+
const min = +e.target.min;
18+
const left =
19+
value * (numWidth / max) -
20+
numLabelWidth / 2 +
21+
scale(value, min, max, 10, -10);
22+
label.style.left = `${left}px`;
23+
label.innerHTML = value;
24+
});

44-custom range slider/style.css

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
@import url("https://fonts.googleapis.com/css2?family=Lato&display=swap");
2+
3+
* {
4+
box-sizing: border-box;
5+
}
6+
7+
body {
8+
background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
9+
font-family: "Lato", sans-serif;
10+
display: flex;
11+
flex-direction: column;
12+
align-items: center;
13+
justify-content: center;
14+
height: 100vh;
15+
overflow: hidden;
16+
margin: 0;
17+
}
18+
19+
h2 {
20+
position: absolute;
21+
top: 10px;
22+
}
23+
24+
.range-container {
25+
position: relative;
26+
}
27+
28+
input[type="range"] {
29+
width: 300px;
30+
margin: 18px 0;
31+
-webkit-appearance: none;
32+
}
33+
34+
input[type="range"]:focus {
35+
outline: none;
36+
}
37+
38+
input[type="range"] + label {
39+
background-color: #fff;
40+
position: absolute;
41+
top: -25px;
42+
left: 110px;
43+
width: 80px;
44+
padding: 5px 0;
45+
text-align: center;
46+
border-radius: 4px;
47+
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
48+
}
49+
50+
/* Chrome & Safari */
51+
input[type="range"]::-webkit-slider-runnable-track {
52+
background: purple;
53+
border-radius: 4px;
54+
width: 100%;
55+
height: 10px;
56+
cursor: pointer;
57+
}
58+
59+
input[type="range"]::-webkit-slider-thumb {
60+
-webkit-appearance: none;
61+
height: 24px;
62+
width: 24px;
63+
background: #fff;
64+
border-radius: 50%;
65+
border: 1px solid purple;
66+
margin-top: -7px;
67+
cursor: pointer;
68+
}
69+
70+
/* Firefox */
71+
input[type="range"]::-moz-range-track {
72+
background: purple;
73+
border-radius: 4px;
74+
width: 100%;
75+
height: 14px;
76+
cursor: pointer;
77+
}
78+
79+
input[type="range"]::-moz-range-thumb {
80+
-webkit-appearance: none;
81+
height: 24px;
82+
width: 24px;
83+
background: #fff;
84+
border-radius: 50%;
85+
border: 1px solid purple;
86+
margin-top: -7px;
87+
cursor: pointer;
88+
}
89+
90+
/* IE */
91+
input[type="range"]::-ms-track {
92+
background: purple;
93+
border-radius: 4px;
94+
width: 100%;
95+
height: 14px;
96+
cursor: pointer;
97+
}
98+
99+
input[type="range"]::-ms-thumb {
100+
-webkit-appearance: none;
101+
height: 24px;
102+
width: 24px;
103+
background: #fff;
104+
border-radius: 50%;
105+
border: 1px solid purple;
106+
margin-top: -7px;
107+
cursor: pointer;
108+
}

0 commit comments

Comments
 (0)