Skip to content

Commit 307e52e

Browse files
Using CSS variables
1 parent 40ae71d commit 307e52e

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

CSS-JS/index.html

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Scoped CSS variables and JS</title>
7+
</head>
8+
9+
<body>
10+
<h2>Update CSS variables with
11+
<span class="hl">JS</span>
12+
</h2>
13+
<div class="controls">
14+
<label for="spacing">
15+
Spacing!
16+
</label>
17+
<input id="spacing" type="range" name="spacing" min="10" max="200" value="10" data-sizing="px">
18+
19+
<label for="blur">
20+
Blur!
21+
</label>
22+
<input id="blur" type="range" name="blur" min="0" max="25" value="10" data-sizing="px">
23+
24+
<label for="base">Base Color</label>
25+
<input id="base" type="color" name="base" value="#ffc600">
26+
</div>
27+
28+
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
29+
30+
<style>
31+
32+
:root{
33+
--base: #ffc600;
34+
--spacing: 10px;
35+
--blur: 10px;
36+
}
37+
38+
img {
39+
padding: var(--spacing);
40+
background: var(--base);
41+
filter: blur(var(--blur));
42+
}
43+
44+
.hl{
45+
color: var(--base);
46+
}
47+
48+
body {
49+
text-align: center;
50+
background: #193549;
51+
color: white;
52+
font-family: 'helvetica neue', sans-serif;
53+
font-weight: 100;
54+
font-size: 50px;
55+
}
56+
57+
.controls {
58+
margin-bottom: 50px;
59+
}
60+
input {
61+
width:100px;
62+
}
63+
</style>
64+
65+
<script>
66+
const inputs = document.querySelectorAll(".controls input");
67+
68+
function handleUpdate() {
69+
// All properties with data- keyword in the html tags
70+
// come under datasets object
71+
const suffix = this.dataset.sizing || '';
72+
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
73+
}
74+
75+
inputs.forEach(input => input.addEventListener('change', handleUpdate));
76+
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));
77+
</script>
78+
</body>
79+
</html>

0 commit comments

Comments
 (0)