Skip to content

Commit 0312639

Browse files
committed
wesbos#3 - CSS Variables
1 parent a13d65c commit 0312639

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

03 - CSS Variables/index-START.html

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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>
810
<h2>Update CSS Variables with <span class='hl'>JS</span></h2>
911

@@ -21,6 +23,17 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
2123
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
2224

2325
<style>
26+
:root {
27+
--base: #ffc600;
28+
--spacing: 10px;
29+
--blur: 10px;
30+
}
31+
32+
img {
33+
padding: var(--spacing);
34+
background: var(--base);
35+
filter: blur(var(--blur));
36+
}
2437

2538
/*
2639
misc styles, nothing to do with CSS variables
@@ -45,7 +58,17 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
4558
</style>
4659

4760
<script>
61+
const inputs = document.querySelectorAll('.controls input');
62+
63+
function handleUpdate() {
64+
const suffix = this.dataset.sizing || '';
65+
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
66+
}
67+
68+
inputs.forEach(input => input.addEventListener('change', handleUpdate));
69+
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));
4870
</script>
4971

5072
</body>
51-
</html>
73+
74+
</html>

0 commit comments

Comments
 (0)