@@ -22,6 +22,22 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
2222
2323 < style >
2424
25+ : root {
26+ --spacing : 10px ;
27+ --blur : 10px ;
28+ --base : # ffc600 ;
29+ }
30+
31+ .hl {
32+ color : var (--base );
33+ }
34+
35+ img {
36+ padding : var (--spacing );
37+ background-color : var (--base );
38+ filter : blur (var (--blur ));
39+ }
40+
2541 /*
2642 misc styles, nothing to do with CSS variables
2743 */
@@ -45,43 +61,20 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
4561 input {
4662 width : 100px ;
4763 }
48-
49- img {
50- border : 0 solid # ffc600 ;
51- }
5264 </ style >
5365
5466 < script >
5567
56- const IMG = document . querySelector ( 'img' ) ;
57- const STYLES = [
58- {
59- property : 'borderWidth' ,
60- value : ( val , unit ) => `${ val } ${ unit } ` ,
61- name : 'spacing'
62- } ,
63- {
64- property : 'filter' ,
65- value : ( val , unit ) => `blur(${ val } ${ unit } )` ,
66- name : 'blur'
67- } ,
68- {
69- property : 'borderColor' ,
70- value : ( val ) => val ,
71- name : 'base'
72- }
73- ] ;
74-
7568 function updateStyles ( ) {
76- let style = STYLES . filter ( style => style . name == this . name ) [ 0 ] ;
77- let val = this . value ;
78- let unit = this . dataset . sizing ;
79-
80- IMG . style [ style . property ] = style . value ( val , unit ) ;
69+ let unit = this . dataset . sizing || '' ;
70+ console . log ( `--${ this . name } ` , this . value + unit ) ;
71+ document . documentElement . style . setProperty ( `--${ this . name } ` , this . value + unit ) ;
8172 }
8273
83- STYLES . forEach ( style => {
84- document . querySelector ( `input[name="${ style . name } "]` ) . addEventListener ( 'change' , updateStyles ) ;
74+ let inputs = document . querySelectorAll ( `input` )
75+
76+ inputs . forEach ( input => {
77+ input . addEventListener ( 'input' , updateStyles ) ;
8578 } ) ;
8679
8780 </ script >
0 commit comments