-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathscript.js
27 lines (17 loc) · 837 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const range = document.getElementById('range');
range.addEventListener('input', (e) => {
const value = +e.target.value;
const label = e.target.nextElementSibling;
const rangeWidth = getComputedStyle(e.target).getPropertyValue('width');
const labelWidth = getComputedStyle(label).getPropertyValue('width');
const numWidth = +rangeWidth.substring(0, rangeWidth.length - 2);
const numLabelWidth = +labelWidth.substring(0, labelWidth.length - 2);
const max = +e.target.max;
const min = +e.target.min;
const left = value * (numWidth / max) - numLabelWidth / 2 + scale(value, min, max, 10, -10);
label.style.left = `${ left }px`;
label.innerHTML = value;
});
const scale = (num, inMin, inMax, outMin, outMax) => {
return (num - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
};