11<!DOCTYPE html>
22< html lang ="en ">
3+
34< head >
45 < meta charset ="UTF-8 ">
56 < title > Scoped CSS Variables and JS</ title >
67 < link rel ="icon " href ="https://fav.farm/🔥 " />
78</ head >
9+
810< body >
911 < h2 > Update CSS Variables with < span class ='hl '> JS</ span > </ h2 >
1012
1113 < div class ="controls ">
1214 < label for ="spacing "> Spacing:</ label >
13- < input id ="spacing " type ="range " name ="spacing " min ="10 " max ="200 " value ="10 " data-sizing ="px ">
15+ < input id ="spacing " type ="range " name ="spacing " min ="10 " max ="200 " value ="10 " data-suffix ="px ">
1416
1517 < label for ="blur "> Blur:</ label >
16- < input id ="blur " type ="range " name ="blur " min ="0 " max ="25 " value ="10 " data-sizing ="px ">
18+ < input id ="blur " type ="range " name ="blur " min ="0 " max ="25 " value ="10 " data-suffix ="px ">
1719
1820 < label for ="base "> Base Color</ label >
1921 < input id ="base " type ="color " name ="base " value ="#ffc600 ">
2022 </ div >
2123
22- < img src ="https://source.unsplash.com/7bwQXzbF6KE/800x500 ">
24+ < img src ="https://theluxuryvacationguide.com/wp-content/uploads/2017/07/5courchevel-saulares-peak.jpg "
25+ style ="width: 800px; ">
2326
2427 < 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+ }
2543
2644 /*
2745 misc styles, nothing to do with CSS variables
@@ -46,7 +64,19 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
4664 </ style >
4765
4866 < script >
67+
68+ const inputs = document . querySelectorAll ( '.controls input' ) ;
69+
70+ function handleUpdate ( ) {
71+ const suffix = this . dataset . suffix || '' ;
72+ document . documentElement . style . setProperty ( `--${ this . name } ` , this . value + suffix )
73+ }
74+
75+ inputs . forEach ( input => input . addEventListener ( 'change' , handleUpdate ) )
76+ inputs . forEach ( input => input . addEventListener ( 'mousemove' , handleUpdate ) )
77+
4978 </ script >
5079
5180</ body >
52- </ html >
81+
82+ </ html >
0 commit comments