File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -21,7 +21,21 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
2121 < img src ="https://source.unsplash.com/7bwQXzbF6KE/800x500 ">
2222
2323 < style >
24+ : root {
25+ --base : # ffc600 ;
26+ --spacing : 10px ;
27+ --blur : 10px ;
28+ }
29+
30+ img {
31+ padding : var (--spacing );
32+ background : var (--base );
33+ filter : blur (var (--blur ));
34+ }
2435
36+ .hl {
37+ color : var (--base );
38+ }
2539 /*
2640 misc styles, nothing to do with CSS variables
2741 */
@@ -53,6 +67,18 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
5367 </ style >
5468
5569 < script >
70+ // NOTE: you can reference any element and existing variables will take
71+ // precedence based on the css cascading/priority
72+ // document.querySelector('h2').style.setProperty('--base', 'blue')
73+ const inputs = document . querySelectorAll ( '.controls input' ) ;
74+
75+ function handleUpdate ( ) {
76+ const suffix = this . dataset . sizing || '' ;
77+ document . documentElement . style . setProperty ( `--${ this . name } ` , `${ this . value } ${ suffix } ` ) ;
78+ }
79+
80+ inputs . forEach ( input => input . addEventListener ( 'change' , handleUpdate ) ) ;
81+ inputs . forEach ( input => input . addEventListener ( 'mousemove' , handleUpdate ) ) ;
5682 </ script >
5783
5884</ body >
You can’t perform that action at this time.
0 commit comments