File tree Expand file tree Collapse file tree 1 file changed +37
-3
lines changed
Expand file tree Collapse file tree 1 file changed +37
-3
lines changed Original file line number Diff line number Diff line change 11<!DOCTYPE html>
22< html lang ="en ">
3+
34< head >
45 < meta charset ="UTF-8 ">
56 < 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
11+ < span class ='hl '> JS</ span >
12+ </ h2 >
913
1014 < div class ="controls ">
1115 < label for ="spacing "> Spacing:</ label >
@@ -21,6 +25,21 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
2125 < img src ="https://source.unsplash.com/7bwQXzbF6KE/800x500 ">
2226
2327 < style >
28+ : root {
29+ --base : # ffc600 ;
30+ --spacing : 10px ;
31+ --blur : 10px ;
32+ }
33+
34+ img {
35+ padding : var (--spacing );
36+ background : var (--base );
37+ filter : blur (var (--blur ));
38+ }
39+
40+ .hl {
41+ color : var (--base );
42+ }
2443
2544 /*
2645 misc styles, nothing to do with CSS variables
@@ -40,12 +59,27 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
4059 }
4160
4261 input {
43- width : 100px ;
62+ width : 100px ;
4463 }
4564 </ style >
4665
4766 < script >
67+ const inputs = document . querySelectorAll ( '.controls input' ) ;
68+
69+ const handleUpdate = e => {
70+ const suffix = e . target . dataset . sizing || '' ;
71+ document . documentElement . style . setProperty ( `--${ e . target . name } ` , e . target . value + suffix ) ;
72+
73+
74+ }
75+ inputs . forEach ( input => {
76+ input . addEventListener ( 'change' , handleUpdate )
77+ input . addEventListener ( 'mousemove' , handleUpdate )
78+ } ) ;
79+
80+
4881 </ script >
4982
5083</ body >
51- </ html >
84+
85+ </ html >
You can’t perform that action at this time.
0 commit comments