|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
3 | | -<head> |
4 | | - <meta charset="UTF-8"> |
5 | | - <title>Scoped CSS Variables and JS</title> |
6 | | -</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 id="spacing" type="range" name="spacing" min="10" max="200" value="10" data-sizing="px"> |
13 | | - |
14 | | - <label for="blur">Blur:</label> |
15 | | - <input id="blur" type="range" name="blur" min="0" max="25" value="10" data-sizing="px"> |
16 | | - |
17 | | - <label for="base">Base Color</label> |
18 | | - <input id="base" type="color" name="base" value="#ffc600"> |
19 | | - </div> |
20 | | - |
21 | | - <img src="https://source.unsplash.com/7bwQXzbF6KE/800x500"> |
22 | | - |
23 | | - <style> |
24 | | - |
25 | | - /* |
26 | | - misc styles, nothing to do with CSS variables |
27 | | - */ |
28 | | - |
29 | | - body { |
30 | | - text-align: center; |
31 | | - background: #193549; |
32 | | - color: white; |
33 | | - font-family: 'helvetica neue', sans-serif; |
34 | | - font-weight: 100; |
35 | | - font-size: 50px; |
36 | | - } |
37 | | - |
38 | | - .controls { |
39 | | - margin-bottom: 50px; |
40 | | - } |
41 | | - |
42 | | - input { |
43 | | - width: 100px; |
44 | | - } |
45 | | - </style> |
46 | | - |
47 | | - <script> |
48 | | - </script> |
49 | | - |
50 | | -</body> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <title>Scoped CSS Variables and JS</title> |
| 6 | + </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 |
| 13 | + id="spacing" |
| 14 | + type="range" |
| 15 | + name="spacing" |
| 16 | + min="10" |
| 17 | + max="200" |
| 18 | + value="10" |
| 19 | + data-sizing="px" |
| 20 | + /> |
| 21 | + |
| 22 | + <label for="blur">Blur:</label> |
| 23 | + <input |
| 24 | + id="blur" |
| 25 | + type="range" |
| 26 | + name="blur" |
| 27 | + min="0" |
| 28 | + max="25" |
| 29 | + value="10" |
| 30 | + data-sizing="px" |
| 31 | + /> |
| 32 | + |
| 33 | + <label for="base">Base Color</label> |
| 34 | + <input id="base" type="color" name="base" value="#ffc600" /> |
| 35 | + </div> |
| 36 | + |
| 37 | + <img src="https://source.unsplash.com/7bwQXzbF6KE/800x500" /> |
| 38 | + |
| 39 | + <style> |
| 40 | + :root { |
| 41 | + --base: #ffc600; |
| 42 | + --spacing: 10px; |
| 43 | + --blur: 10px; |
| 44 | + } |
| 45 | + |
| 46 | + img { |
| 47 | + padding: var(--spacing); |
| 48 | + background: var(--base); |
| 49 | + filter: blur(var(--blur)); |
| 50 | + } |
| 51 | + |
| 52 | + .h1 { |
| 53 | + color: var(--base); |
| 54 | + } |
| 55 | + |
| 56 | + /* |
| 57 | + misc styles, nothing to do with CSS variables |
| 58 | + */ |
| 59 | + |
| 60 | + body { |
| 61 | + text-align: center; |
| 62 | + background: #193549; |
| 63 | + color: white; |
| 64 | + font-family: 'helvetica neue', sans-serif; |
| 65 | + font-weight: 100; |
| 66 | + font-size: 50px; |
| 67 | + } |
| 68 | + |
| 69 | + .controls { |
| 70 | + margin-bottom: 50px; |
| 71 | + } |
| 72 | + |
| 73 | + input { |
| 74 | + width: 100px; |
| 75 | + } |
| 76 | + </style> |
| 77 | + |
| 78 | + <script> |
| 79 | + const inputs = document.querySelectorAll('.controls input'); |
| 80 | + |
| 81 | + function handleUpdate() { |
| 82 | + const suffix = this.dataset.sizing || ''; |
| 83 | + document.documentElement.style.setProperty( |
| 84 | + `--${this.name}`, |
| 85 | + this.value + suffix |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + inputs.forEach(input => |
| 90 | + input.addEventListener('change', handleUpdate) |
| 91 | + ); |
| 92 | + inputs.forEach(input => |
| 93 | + input.addEventListener('mousemove', handleUpdate) |
| 94 | + ); |
| 95 | + </script> |
| 96 | + </body> |
51 | 97 | </html> |
0 commit comments