File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Scoped CSS Variables and JS</ title >
6+ </ head >
7+ < body >
8+ < h2 > Update CSS Variables with < span class ='hl '> JS</ span > </ h2 >
9+
10+ < div class ="controls ">
11+ < label for ="spacing "> Spacing:</ label >
12+ < input autocomplete ="off " type ="range " name ="spacing " min ="10 " max ="200 " value ="10 " data-sizing ="px ">
13+
14+ < label for ="blur "> Blur:</ label >
15+ < input autocomplete ="off " type ="range " name ="blur " min ="0 " max ="25 " value ="10 " data-sizing ="px ">
16+
17+ < label for ="base "> Base Color</ label >
18+ < input autocomplete ="off " type ="color " name ="base " value ="#ffc600 ">
19+ </ div >
20+
21+ < img src ="https://source.unsplash.com/7bwQXzbF6KE/800x500 ">
22+
23+ < 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+ }
35+
36+ .hl {
37+ color : var (--base );
38+ }
39+ /*
40+ misc styles, nothing to do with CSS variables
41+ */
42+
43+ body {
44+ text-align : center;
45+ }
46+
47+ body {
48+ background : # 193549 ;
49+ color : white;
50+ font-family : 'helvetica neue' , sans-serif;
51+ font-weight : 100 ;
52+ font-size : 50px ;
53+ }
54+
55+ .controls {
56+ margin-bottom : 50px ;
57+ }
58+
59+ a {
60+ color : var (--base );
61+ text-decoration : none;
62+ }
63+
64+ input {
65+ width : 100px ;
66+ }
67+ </ style >
68+ < script >
69+
70+ function handleUpdate ( ) {
71+ const suffix = this . dataset . sizing || '' ;
72+ document . documentElement . style . setProperty ( `--${ this . name } ` , this . value + suffix ) ;
73+ }
74+
75+ for ( var input of document . querySelectorAll ( '.controls input' ) ) {
76+ input . addEventListener ( 'change' , handleUpdate )
77+ input . addEventListener ( 'mousemove' , handleUpdate )
78+ }
79+
80+ </ script >
81+
82+ </ body >
83+ </ html >
You can’t perform that action at this time.
0 commit comments