Skip to content

Commit 6d3e0cd

Browse files
author
=
committed
JS30 wesbos#3 Completed
1 parent fb0cb34 commit 6d3e0cd

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

03 - CSS Variables/index-START.html

Lines changed: 28 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,10 +23,24 @@ <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+
--spacing: 10px;
28+
--base: #ffc600;
29+
--blur: 10px;
30+
}
2431

2532
/*
2633
misc styles, nothing to do with CSS variables
2734
*/
35+
img {
36+
padding: var(--spacing);
37+
filter: blur(var(--blur));
38+
background: var(--base);
39+
}
40+
41+
.hl {
42+
color: var(--base);
43+
}
2844

2945
body {
3046
text-align: center;
@@ -45,7 +61,18 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
4561
</style>
4662

4763
<script>
64+
const inputs = document.querySelectorAll('.controls input');
65+
function handleUpdate() {
66+
if (this.name == "base")
67+
document.documentElement.style.setProperty(`--${this.name}`, this.value);
68+
else
69+
document.documentElement.style.setProperty(`--${this.name}`, this.value + "px");
70+
}
71+
72+
inputs.forEach(input => input.addEventListener('change', handleUpdate));
73+
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));
4874
</script>
4975

5076
</body>
51-
</html>
77+
78+
</html>

0 commit comments

Comments
 (0)