Skip to content

Commit d51d6e7

Browse files
committed
Playing with variables
Signed-off-by: Ninerian <hallo@daniel-der-grosse.de>
1 parent 48aebd7 commit d51d6e7

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

03 - CSS Variables/index-START.html

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Scoped CSS Variables and JS</title>
67
</head>
8+
79
<body>
8-
<h2>Update CSS Variables with <span class='hl'>JS</span></h2>
10+
<h2>Update CSS Variables with
11+
<span class='hl'>JS</span>
12+
</h2>
913

1014
<div class="controls">
1115
<label for="spacing">Spacing:</label>
@@ -21,6 +25,21 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
2125
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
2226

2327
<style>
28+
:root {
29+
--base: #ffc600;
30+
--spacing: 10px;
31+
--blur: 10px;
32+
}
33+
34+
img {
35+
padding: var(--spacing);
36+
background: var(--base);
37+
filter: blur(var(--blur));
38+
}
39+
40+
.hl {
41+
color: var(--base);
42+
}
2443

2544
/*
2645
misc styles, nothing to do with CSS variables
@@ -40,12 +59,27 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
4059
}
4160

4261
input {
43-
width:100px;
62+
width: 100px;
4463
}
4564
</style>
4665

4766
<script>
67+
const inputs = document.querySelectorAll('.controls input');
68+
69+
const handleUpdate = e => {
70+
const suffix = e.target.dataset.sizing || '';
71+
document.documentElement.style.setProperty(`--${e.target.name}`, e.target.value + suffix);
72+
73+
74+
}
75+
inputs.forEach(input => {
76+
input.addEventListener('change', handleUpdate)
77+
input.addEventListener('mousemove', handleUpdate)
78+
});
79+
80+
4881
</script>
4982

5083
</body>
51-
</html>
84+
85+
</html>

0 commit comments

Comments
 (0)