Skip to content

Commit a6acaf5

Browse files
committed
Did day 3 - learned about CSS Variables and more DOM manipulation
1 parent 936643c commit a6acaf5

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

03 - CSS Variables/index-CF.html

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Scoped CSS Variables and JS</title>
6+
</head>
7+
<body>
8+
<h2>Update CSS Variables with <span class='hl'>JS</span></h2>
9+
10+
<div class="controls">
11+
<label for="spacing">Spacing:</label>
12+
<input type="range" name="spacing" min="10" max="200" value="10" data-sizing="px">
13+
14+
<label for="blur">Blur:</label>
15+
<input type="range" name="blur" min="0" max="25" value="10" data-sizing="px">
16+
17+
<label for="base">Base Color</label>
18+
<input type="color" name="base" value="#ffc600">
19+
</div>
20+
21+
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
22+
23+
<style>
24+
:root {
25+
--base: #ffc600;
26+
--spacing: 10px;
27+
--blur: 10px;
28+
}
29+
30+
img {
31+
padding: var(--spacing);
32+
background: var(--base);
33+
filter: blur(var(--blur));
34+
}
35+
36+
.hl {
37+
color: var(--base);
38+
}
39+
40+
/*
41+
misc styles, nothing to do with CSS variables
42+
*/
43+
44+
body {
45+
text-align: center;
46+
}
47+
48+
body {
49+
background: #193549;
50+
color: white;
51+
font-family: 'helvetica neue', sans-serif;
52+
font-weight: 100;
53+
font-size: 50px;
54+
}
55+
56+
.controls {
57+
margin-bottom: 50px;
58+
}
59+
60+
a {
61+
color: var(--base);
62+
text-decoration: none;
63+
}
64+
65+
input {
66+
width:100px;
67+
}
68+
69+
70+
</style>
71+
72+
<script>
73+
74+
//querySelectorAll can apparently accept hierarchy selection like css: Below, querySelectorAll will select all the input elements inside an element with the class ".control"
75+
const inputs = document.querySelectorAll('.controls input');
76+
77+
function handleUpdate() {
78+
const suffix = this.dataset.sizing || '';
79+
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
80+
}
81+
82+
inputs.forEach(input => input.addEventListener('change', handleUpdate));
83+
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));
84+
85+
</script>
86+
87+
</body>
88+
</html>

0 commit comments

Comments
 (0)