File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,24 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
2121 < img src ="https://source.unsplash.com/7bwQXzbF6KE/800x500 ">
2222
2323 < style >
24+ /*
25+ define base styles for default values
26+ */
27+ : root {
28+ --base : # ffc600 ;
29+ --spacing : 10px ;
30+ --blur : 10px ;
31+ }
32+
33+ img {
34+ background : var (--base );
35+ filter : var (--blur );
36+ padding : var (--spacing );
37+ }
38+
39+ .hl {
40+ color : var (--base );
41+ }
2442
2543 /*
2644 misc styles, nothing to do with CSS variables
@@ -48,6 +66,16 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
4866 </ style >
4967
5068 < script >
69+ const inputs = document . querySelectorAll ( '.controls input' ) ; // find all inputs
70+
71+ function handleUpdate ( ) {
72+ console . log ( this . value ) ;
73+ const suffix = this . dataset . sizing || '' ; // use a prefix data- attribute (spacing, blur) or nothing (hex color)
74+ document . documentElement . style . setProperty ( `--${ this . name } ` , this . value + suffix ) ;
75+ }
76+
77+ inputs . forEach ( input => input . addEventListener ( 'change' , handleUpdate ) )
78+ inputs . forEach ( input => input . addEventListener ( 'mousemove' , handleUpdate ) )
5179 </ script >
5280
5381</ body >
You can’t perform that action at this time.
0 commit comments