Skip to content

Commit 7844b15

Browse files
committed
css variables done
1 parent 60ef880 commit 7844b15

File tree

1 file changed

+65
-33
lines changed

1 file changed

+65
-33
lines changed
Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,86 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
4-
<meta charset="UTF-8">
5-
<title>Scoped CSS Variables and JS</title>
5+
<meta charset="UTF-8">
6+
<title>Scoped CSS Variables and JS</title>
67
</head>
8+
79
<body>
8-
<h2>Update CSS Variables with <span class='hl'>JS</span></h2>
10+
<h2>Update CSS Variables with <span class='hl'>JS</span></h2>
911

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">
12+
<div class="controls">
13+
<label for="spacing">Spacing:</label>
14+
<input type="range" name="spacing" min="10" max="200" value="10" data-sizing="px">
1315

14-
<label for="blur">Blur:</label>
15-
<input type="range" name="blur" min="0" max="25" value="10" data-sizing="px">
16+
<label for="blur">Blur:</label>
17+
<input type="range" name="blur" min="0" max="25" value="10" data-sizing="px">
1618

17-
<label for="base">Base Color</label>
18-
<input type="color" name="base" value="#ffc600">
19-
</div>
19+
<label for="base">Base Color</label>
20+
<input type="color" name="base" value="#ffc600">
21+
</div>
2022

21-
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
23+
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500">
2224

23-
<style>
25+
<style>
26+
:root {
27+
--base: #ffc600;
28+
--spacing: 10px;
29+
--blur: 10px;
30+
}
2431

25-
/*
32+
img {
33+
padding: var(--spacing);
34+
/*-- analogicznie jak w js $*/
35+
background: var(--base);
36+
filter: blur(var(--blur));
37+
}
38+
39+
.hl {
40+
color: var(--base);
41+
}
42+
/*
2643
misc styles, nothing to do with CSS variables
2744
*/
2845

29-
body {
30-
text-align: center;
31-
}
46+
body {
47+
text-align: center;
48+
}
49+
50+
body {
51+
background: #193549;
52+
color: white;
53+
font-family: 'helvetica neue', sans-serif;
54+
font-weight: 100;
55+
font-size: 50px;
56+
}
3257

33-
body {
34-
background: #193549;
35-
color: white;
36-
font-family: 'helvetica neue', sans-serif;
37-
font-weight: 100;
38-
font-size: 50px;
39-
}
58+
.controls {
59+
margin-bottom: 50px;
60+
}
4061

41-
.controls {
42-
margin-bottom: 50px;
43-
}
62+
input {
63+
width: 100px;
64+
}
65+
</style>
4466

45-
input {
46-
width:100px;
47-
}
48-
</style>
67+
<script>
68+
const inputs = document.querySelectorAll('.controls input');
69+
console.log(inputs); // otrzymuje NodeList - wygląda jak tablica, ale nie ma po rozwinięciu _proto w konsoli wszystkich metod tablicowych
4970

50-
<script>
51-
</script>
71+
function handleUpdate() {
72+
console.log(this.value); //zobaczymy w konsoli konkretną wartość, która aktualnie się zmienia
73+
console.log(this.dataset); //atrybut dataset jako objekt
74+
console.log(this.name); //pokazuje wartość atrybutu name
75+
const suffix = this.dataset.sizing || ''; //zmienna dla suwaka, wskazująca wartość w px lub pustą wartość dla koloru
76+
77+
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix); //--spacing/or --blur + this.value (bez suffixa nie pokazywałoby wartości w px)
78+
79+
}
80+
inputs.forEach(input => input.addEventListener('change', handleUpdate));
81+
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));
82+
</script>
5283

5384
</body>
85+
5486
</html>

0 commit comments

Comments
 (0)