-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (66 loc) · 2.29 KB
/
script.js
File metadata and controls
78 lines (66 loc) · 2.29 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
function isOlderEdgeOrIE() {
return (
window.navigator.userAgent.indexOf("MSIE ") > -1 ||
!!navigator.userAgent.match(/Trident.*rv\:11\./) ||
window.navigator.userAgent.indexOf("Edge") > -1
);
}
function valueTotalRatio(value, min, max) {
return ((value - min)*0.1 / (max - min)).toFixed(2);
}
function getLinearGradientCSS(ratio, leftColor, rightColor) {
return [
'-webkit-gradient(',
'linear, ',
'left top, ',
'right top, ',
'color-stop(' + ratio + ', ' + leftColor + '), ',
'color-stop(' + ratio + ', ' + rightColor + ')',
')'
].join('');
}
function updateRangeEl(rangeEl) {
rangeEl.forEach(e => {
var ratio = valueTotalRatio(e.value, e.min, e.max);
e.style.backgroundImage = getLinearGradientCSS(ratio, '#fb6470', '#bcbcbe');
})
}
function initRangeEl() {
var rangeEl = document.querySelectorAll('input[type=range]');
var textEl = document.querySelector('input[type=text]');
if (isOlderEdgeOrIE()) {
rangeEl.style.height = "20px";
// IE 11/10 fires change instead of input
// https://stackoverflow.com/a/50887531/3528132
rangeEl.forEach(elem => elem.addEventListener("change", function(e) {
textEl.value = e.target.value;
}));
rangeEl.forEach(elem => elem.addEventListener("input", function(e) {
textEl.value = e.target.value;
}));
} else {
updateRangeEl(rangeEl);
rangeEl.forEach(elem => elem.addEventListener("input", function(e) {
updateRangeEl(e.target);
textEl.value = e.target.value;
}));
}
}
initRangeEl(); */
const rangeInputs = document.querySelectorAll('input[type="range"]')
const numberInput = document.querySelector('input[type="number"]')
function handleInputChange(e) {
let target = e.target
if (e.target.type !== 'range') {
target = document.getElementById('range')
}
const min = target.min
const max = target.max
const val = target.value
target.style.backgroundSize = (val - min) * 100 / (max - min) + '% 100%'
}
rangeInputs.forEach(input => {
input.addEventListener('input', handleInputChange)
})
numberInput.addEventListener('input', handleInputChange)