|
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> |
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 type="range" name="spacing" min="10" max="200" value="10" data-sizing="px"> |
13 | | - |
14 | | - <label for="blur">Blur:</label> |
15 | | - <input type="range" name="blur" min="0" max="25" value="10" data-sizing="px"> |
16 | | - |
17 | | - <label for="base">Base Color</label> |
18 | | - <input type="color" name="base" value="#ffc600"> |
19 | | - </div> |
20 | | - |
21 | | - <img src="https://source.unsplash.com/7bwQXzbF6KE/800x500"> |
22 | | - |
23 | | - <style> |
24 | 8 |
|
| 9 | +<body> |
| 10 | + <h2>Update CSS Variables with <span class='hl'>JS</span></h2> |
| 11 | + <div class="controls"> |
| 12 | + <label for="spacing">Spacing:</label> |
| 13 | + <input type="range" name="spacing" min="10" max="200" value="10" data-sizing="px"> |
| 14 | + <label for="blur">Blur:</label> |
| 15 | + <input type="range" name="blur" min="0" max="25" value="10" data-sizing="px"> |
| 16 | + <label for="base">Base Color</label> |
| 17 | + <input type="color" name="base" value="#ffc600"> |
| 18 | + </div> |
| 19 | + <img src="https://source.unsplash.com/7bwQXzbF6KE/800x500"> |
| 20 | + <style> |
| 21 | + :root { |
| 22 | + --base: #ffc600; |
| 23 | + --blur: 10px; |
| 24 | + --spacing: 10px; |
| 25 | + } |
| 26 | + |
| 27 | + img { |
| 28 | + padding: var(--spacing); |
| 29 | + filter: blur(var(--blur)); |
| 30 | + background: var(--base); |
| 31 | + } |
| 32 | + |
| 33 | + .hl { |
| 34 | + color: var(--base); |
| 35 | + } |
25 | 36 | /* |
26 | 37 | misc styles, nothing to do with CSS variables |
27 | 38 | */ |
28 | | - |
| 39 | + |
29 | 40 | body { |
30 | | - text-align: center; |
| 41 | + text-align: center; |
31 | 42 | } |
32 | | - |
| 43 | + |
33 | 44 | body { |
34 | | - background: #193549; |
35 | | - color: white; |
36 | | - font-family: 'helvetica neue', sans-serif; |
37 | | - font-weight: 100; |
38 | | - font-size: 50px; |
| 45 | + background: #193549; |
| 46 | + color: white; |
| 47 | + font-family: 'helvetica neue', sans-serif; |
| 48 | + font-weight: 100; |
| 49 | + font-size: 50px; |
39 | 50 | } |
40 | | - |
| 51 | + |
41 | 52 | .controls { |
42 | | - margin-bottom: 50px; |
| 53 | + margin-bottom: 50px; |
43 | 54 | } |
44 | | - |
| 55 | + |
45 | 56 | a { |
46 | | - color: var(--base); |
47 | | - text-decoration: none; |
| 57 | + color: var(--base); |
| 58 | + text-decoration: none; |
48 | 59 | } |
49 | | - |
| 60 | + |
50 | 61 | input { |
51 | | - width:100px; |
| 62 | + width: 100px; |
52 | 63 | } |
53 | | - </style> |
| 64 | + </style> |
| 65 | + <script> |
54 | 66 |
|
55 | | - <script> |
56 | | - </script> |
| 67 | + const inputs = document.querySelectorAll('.controls input'); |
| 68 | + inputs.forEach(input => { |
| 69 | + input.addEventListener('change', handleUpdate); |
| 70 | + input.addEventListener('mousemove', handleUpdate); |
| 71 | + }); |
57 | 72 |
|
| 73 | + function handleUpdate(e) { |
| 74 | + const suffix = this.dataset.sizing || ''; |
| 75 | + document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix); |
| 76 | + } |
| 77 | + |
| 78 | + </script> |
58 | 79 | </body> |
| 80 | + |
59 | 81 | </html> |
0 commit comments