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