Skip to content

Commit 077c3d7

Browse files
committed
Day 3 done
1 parent 4d2f1be commit 077c3d7

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

03 - CSS Variables/index-START.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
2222

2323
<style>
2424

25+
/* :root is the highest you can go - nothing is more base than :root */
26+
:root {
27+
/* variable names are prefixed with -- ... that's just the spec. */
28+
--base: #ffc600;
29+
--spacing: 10px;
30+
--blur: 10px;
31+
}
32+
33+
img {
34+
/* fetch the value using var() to sub in the CSS var in this place */
35+
padding: var(--spacing);
36+
background: var(--base);
37+
filter: blur(var(--blur));
38+
}
39+
40+
.hl {
41+
color: var(--base);
42+
}
43+
2544
/*
2645
misc styles, nothing to do with CSS variables
2746
*/
@@ -48,6 +67,28 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
4867
</style>
4968

5069
<script>
70+
const inputs = document.querySelectorAll('.controls input');
71+
72+
function handleUpdate() {
73+
// use the HTML attrib data attrs, e.g. data-sizing, to determine the
74+
// unit (the slider just says e.g. "10" not "10px").
75+
76+
// dataset has all the "data-" attributes on the element. Handy!
77+
const suffix = this.dataset.sizing || ''; // alt. case in case no data-sizing
78+
79+
// update the value, to affect the DOM and produce the visible effect
80+
// name is taken from the name attr on the input element. Same for value
81+
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
82+
}
83+
84+
// listen for a change; this is when you change the value. On a slider,
85+
// it's when you let go and the new value is set.
86+
inputs.forEach(input => input.addEventListener('change', handleUpdate));
87+
88+
// listen for mousemove also. This will fire as you slide a slider
89+
// around, even without letting go. So this is how we get the behavior
90+
// of scrubbing across the slider and the values are changing in realtime.
91+
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));
5192
</script>
5293

5394
</body>

0 commit comments

Comments
 (0)