Skip to content

Commit 0e4e274

Browse files
committed
css variable
1 parent e35764e commit 0e4e274

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

03 - CSS Variables/index-START.html

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,22 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
2121
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
2222

2323
<style>
24+
/* css variable*/
25+
:root {
26+
--base: #ffc600;
27+
--spacing: 10px;
28+
--blur: 10px;
29+
}
2430

25-
/*
26-
misc styles, nothing to do with CSS variables
27-
*/
31+
img {
32+
padding: var(--spacing);
33+
background: var(--base);
34+
filter: blur(var(--blur));
35+
}
36+
37+
.hl {
38+
color: var(--base);
39+
}
2840

2941
body {
3042
text-align: center;
@@ -48,6 +60,17 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
4860
</style>
4961

5062
<script>
63+
// node list
64+
// this is not an array
65+
var inputs = document.querySelectorAll('.controls input');
66+
function handleUpdate() {
67+
var suffix = this.dataset.sizing || '';// object
68+
console.log(this.dataset);
69+
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
70+
}
71+
72+
inputs.forEach(input => input.addEventListener('change', handleUpdate));
73+
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));
5174
</script>
5275

5376
</body>

0 commit comments

Comments
 (0)