-
Notifications
You must be signed in to change notification settings - Fork 38
/
RangeSlider.js
39 lines (31 loc) · 1.1 KB
/
RangeSlider.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
28
29
30
31
32
33
34
35
36
37
38
39
const inputValueDescriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value");
const RangeSlider = {
inputHandle(event) {
const inputRange = event.target.closest("input[type='range']");
if (inputRange) {
RangeSlider.sliderVisualCalc(inputRange);
}
},
sliderVisualCalc(slider) {
const min = parseFloat(slider.min) || 0;
const max = parseFloat(slider.max) || 100;
const value = parseFloat(slider.value) || 0;
slider.style.backgroundSize = (100 * ((value - min) / (max - min))) + "% 100%";
if (slider.classList.contains("slider-vertical")) {
slider.style.marginBottom = slider.getBoundingClientRect().width + "px";
}
},
__inputValuePropertySetter(value) {
inputValueDescriptor.set.apply(this, [value]);
if (this.type === "range") {
RangeSlider.sliderVisualCalc(this);
return true;
}
}
};
Object.defineProperty(HTMLInputElement.prototype, "value", {
get: inputValueDescriptor.get,
set: RangeSlider.__inputValuePropertySetter
});
window.addEventListener("input", RangeSlider.inputHandle);
module.exports = RangeSlider;