|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
| 3 | + |
3 | 4 | <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> |
6 | 7 | </head> |
| 8 | + |
7 | 9 | <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> |
9 | 11 |
|
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"> |
13 | 15 |
|
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"> |
16 | 18 |
|
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> |
20 | 22 |
|
21 | | - <img src="https://source.unsplash.com/7bwQXzbF6KE/800x500"> |
| 23 | + <img src="https://source.unsplash.com/7bwQXzbF6KE/800x500"> |
22 | 24 |
|
23 | | - <style> |
| 25 | + <style> |
| 26 | + :root { |
| 27 | + --base: #ffc600; |
| 28 | + --spacing: 10px; |
| 29 | + --blur: 10px; |
| 30 | + } |
24 | 31 |
|
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 | + /* |
26 | 43 | misc styles, nothing to do with CSS variables |
27 | 44 | */ |
28 | 45 |
|
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 | + } |
32 | 57 |
|
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 | + } |
40 | 61 |
|
41 | | - .controls { |
42 | | - margin-bottom: 50px; |
43 | | - } |
| 62 | + input { |
| 63 | + width: 100px; |
| 64 | + } |
| 65 | + </style> |
44 | 66 |
|
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 |
49 | 70 |
|
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> |
52 | 83 |
|
53 | 84 | </body> |
| 85 | + |
54 | 86 | </html> |
0 commit comments