Skip to content

Commit f537832

Browse files
authored
Day 44
1 parent 0d26e30 commit f537832

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

project 44/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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" id="range" min="0" max="100" />
13+
<label for="range">50</label>
14+
</div>
15+
16+
<script src="script.js"></script>
17+
</body>
18+
</html>

project 44/script.js

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

project 44/style.css

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
@import url('https://fonts.googleapis.com/css?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: 13px;
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: 13px;
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)