44 < meta charset ="UTF-8 ">
55 < title > Scoped CSS Variables and JS</ title >
66</ head >
7- < body >
7+ < body id =" body " >
88 < h2 > Update CSS Variables with < span class ='hl '> JS</ span > </ h2 >
99
1010 < div class ="controls ">
@@ -26,17 +26,25 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
2626 misc styles, nothing to do with CSS variables
2727 */
2828
29+ : root {
30+ --base-color : # 193549 ;
31+ }
32+
2933 body {
3034 text-align : center;
31- background : # 193549 ;
35+ background : var ( --base-color ) ;
3236 color : white;
3337 font-family : 'helvetica neue' , sans-serif;
3438 font-weight : 100 ;
3539 font-size : 50px ;
3640 }
3741
3842 .controls {
39- margin-bottom : 50px ;
43+ margin-bottom : var (--margin-bottom );
44+ }
45+
46+ img {
47+ filter : blur (var (--blur ));
4048 }
4149
4250 input {
@@ -45,6 +53,39 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
4553 </ style >
4654
4755 < script >
56+
57+ function updateSpacing ( e ) {
58+ const cssSpacingVar = "--margin-bottom" ;
59+ const targets = document . getElementsByClassName ( "controls" ) ;
60+ const targetsArr = Array . from ( targets ) ;
61+ targetsArr . forEach ( element => {
62+ element . style . setProperty ( cssSpacingVar , this . value + 'px' ) ;
63+ } ) ;
64+ }
65+
66+ function updateBlur ( e ) {
67+ const cssBlur = "--blur" ;
68+ const targets = document . getElementsByTagName ( "img" ) ;
69+ const targetsArr = Array . from ( targets ) ;
70+ targetsArr . forEach ( element => {
71+ element . style . setProperty ( cssBlur , this . value + 'px' ) ;
72+ } ) ;
73+ }
74+
75+ function updateBaseColor ( e ) {
76+ const cssBaseColor = "--base-color" ;
77+ const body = document . getElementById ( "body" ) ;
78+ body . style . setProperty ( cssBaseColor , this . value ) ;
79+ }
80+
81+ const spacingInput = document . getElementById ( "spacing" ) ;
82+ spacingInput . addEventListener ( 'click' , updateSpacing ) ;
83+
84+ const blurInput = document . getElementById ( "blur" ) ;
85+ blurInput . addEventListener ( 'click' , updateBlur ) ;
86+
87+ const baseColorInput = document . getElementById ( "base" ) ;
88+ baseColorInput . addEventListener ( 'click' , updateBaseColor ) ;
4889 </ script >
4990
5091</ body >
0 commit comments