File tree Expand file tree Collapse file tree 1 file changed +85
-0
lines changed Expand file tree Collapse file tree 1 file changed +85
-0
lines changed 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+ < script src ="http://localhost:35729/livereload.js "> </ script >
7+ </ head >
8+ < body onload ="init() ">
9+ < h2 > Update CSS Variables with < span class ='hl '> JS</ span > </ h2 >
10+
11+ < div class ="controls ">
12+ < label for ="spacing "> Spacing:</ label >
13+ < input type ="range " name ="spacing " min ="10 " max ="200 " value ="10 " data-sizing ="px ">
14+
15+ < label for ="blur "> Blur:</ label >
16+ < input type ="range " name ="blur " min ="0 " max ="25 " value ="10 " data-sizing ="px ">
17+
18+ < label for ="base "> Base Color</ label >
19+ < input type ="color " name ="base " value ="#ffc600 ">
20+ </ div >
21+
22+ < img src ="https://source.unsplash.com/7bwQXzbF6KE/800x500 ">
23+
24+ < style >
25+ : root {
26+ --base;
27+ - - spacing;
28+ - - blur;
29+ }
30+ /*
31+ misc styles, nothing to do with CSS variables
32+ */
33+ img {
34+ padding : var (--spacing );
35+ background : var (--base );
36+ filter : blur (var (--blur ));
37+ }
38+
39+ .hl {
40+ color : var (--base );
41+ }
42+ body {
43+ text-align : center;
44+ }
45+
46+ body {
47+ background : # 193549 ;
48+ color : white;
49+ font-family : 'helvetica neue' , sans-serif;
50+ font-weight : 100 ;
51+ font-size : 50px ;
52+ }
53+
54+ .controls {
55+ margin-bottom : 50px ;
56+ }
57+
58+ a {
59+ color : var (--base );
60+ text-decoration : none;
61+ }
62+
63+ input {
64+ width : 100px ;
65+ }
66+ </ style >
67+
68+ < script >
69+ const inputs = document . querySelectorAll ( '.controls input' ) ;
70+ inputs . forEach ( elm => elm . addEventListener ( 'change' , handleUpdate ) ) ;
71+ inputs . forEach ( elm => elm . addEventListener ( 'mousemove' , handleUpdate ) ) ;
72+ function handleUpdate ( ) {
73+ if ( ! this . dataset ) {
74+ return ;
75+ }
76+ const suffix = this . dataset . sizing || '' ;
77+ document . documentElement . style . setProperty ( `--${ this . name } ` , this . value + suffix ) ;
78+ }
79+ function init ( ) {
80+ inputs . forEach ( elm => handleUpdate . apply ( elm ) ) ;
81+ }
82+ </ script >
83+
84+ </ body >
85+ </ html >
You can’t perform that action at this time.
0 commit comments