forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-START.html
More file actions
106 lines (79 loc) · 2.11 KB
/
index-START.html
File metadata and controls
106 lines (79 loc) · 2.11 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Scoped CSS Variables and JS</title>
</head>
<body>
<h2>Update CSS Variables with <span class='hl'>JS</span></h2>
<div class="controls">
<label for="spacing">Spacing:</label>
<input type="range" name="spacing" min="10" max="200" value="10" data-sizing="px">
<label for="blur">Blur:</label>
<input type="range" name="blur" min="0" max="25" value="10" data-sizing="px">
<label for="base">Base Color</label>
<input type="color" name="base" value="#ffc600">
</div>
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
<style>
/*
misc styles, nothing to do with CSS variables
*/
img {
transition: all .5s;
}
body {
text-align: center;
}
body {
background: #193549;
color: white;
font-family: 'helvetica neue', sans-serif;
font-weight: 100;
font-size: 50px;
}
.controls {
margin-bottom: 50px;
}
a {
color: var(--base);
text-decoration: none;
}
input {
width:100px;
}
</style>
<script>
let inputEls = Array.prototype
.slice.call( document.querySelectorAll('input') );
let image = document.querySelector('img');
inputEls.forEach( el => {
el.addEventListener( 'change', getInputVal );
handleValChange( el.name, el.value );
});
function handleValChange( name, val ) {
let style = image.style;
switch( name ) {
case 'spacing':
let bgColor = document.querySelector(`input[name='base']`).value;
image.style.border = `${val}px solid ${bgColor}`;
break;
case 'blur':
image.style.filter = `blur(${val}px)`;
break;
case 'base':
image.style['border-color'] = `${val}`;
break;
default:
console.log( 'you fucked up' );
}
}
function getInputVal( ev ) {
if ( !!~inputEls.indexOf( ev.currentTarget ) ) {
var { value, name } = ev.currentTarget;
handleValChange( name, value );
}
}
</script>
</body>
</html>