Skip to content

Commit 1157192

Browse files
committed
Create css-variables.html
1 parent dc49479 commit 1157192

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 id="spacing" type="range" name="spacing" min="10" max="200" value="10" data-sizing="px">
13+
14+
<label for="blur">Blur:</label>
15+
<input id="blur" type="range" name="blur" min="0" max="25" value="10" data-sizing="px">
16+
17+
<label for="base">Base Color</label>
18+
<input id="base" type="color" name="base" value="#ffc600">
19+
</div>
20+
21+
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
22+
23+
<style>
24+
25+
:root {
26+
--spacing: 10px;
27+
--blur: 10px;
28+
--base: #ffc600;
29+
30+
}
31+
32+
img {
33+
padding: var(--spacing);
34+
background: var(--base);
35+
filter: blur(var(--blur));
36+
}
37+
38+
.hl {
39+
color: var(--base);
40+
}
41+
42+
body {
43+
text-align: center;
44+
background: #193549;
45+
color: white;
46+
font-family: 'helvetica neue', sans-serif;
47+
font-weight: 100;
48+
font-size: 50px;
49+
}
50+
51+
.controls {
52+
margin-bottom: 50px;
53+
}
54+
55+
input {
56+
width: 100px;
57+
}
58+
</style>
59+
60+
<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));
70+
71+
</script>
72+
73+
</body>
74+
</html>

0 commit comments

Comments
 (0)