1
1
<!DOCTYPE html>
2
2
< html lang ="en ">
3
-
4
3
< head >
5
4
< meta charset ="UTF-8 ">
6
5
< title > Scoped CSS Variables and JS</ title >
7
-
8
6
< style >
9
- /*
10
- Define variables at the root level so their scope is the entire document
11
- */
7
+ /* Define variables at the root level so their scope is the entire document */
12
8
: root {
13
9
--base : goldenrod;
14
10
--blur : 10px ;
15
11
--padding : 10px ;
16
12
}
17
13
18
- /*
19
- Set image property values as the variables defined at the root element
20
- */
14
+ /* Set image property values using previously defined variables */
21
15
img {
16
+ box-sizing : border-box;
22
17
background : var (--base );
23
18
filter : blur (var (--blur ));
24
19
padding : var (--padding );
20
+ border-radius : 2rem ;
25
21
}
26
22
23
+ /* 'JS' text color that will match the image background color */
27
24
.hl {
28
25
color : var (--base );
29
26
}
49
46
}
50
47
</ style >
51
48
</ head >
52
-
53
49
< body >
54
50
< h2 > Update CSS Variables with < span class ='hl '> JS</ span > </ h2 >
55
51
@@ -64,26 +60,43 @@ <h2>Update CSS Variables with <span class='hl'>JS</span></h2>
64
60
< input type ="color " name ="base " value ="#ffc600 ">
65
61
</ div >
66
62
67
- < img src ="https://source.unsplash.com/7bwQXzbF6KE/ 800x500 ">
63
+ < img src ="https://source.unsplash.com/800x500 ">
68
64
69
65
< script >
70
- // Declare & define variable reference to all inputs inside the 'control' class
71
- const inputs = document . querySelectorAll ( '.controls input' ) ;
72
-
73
- // Event Handler function
74
- function handleUpdate ( ) {
75
- // Get the data-sizing property value if one exists, or set as empty string
76
- const suffix = this . dataset . sizing || '' ;
77
- // Target the entire document and update the value of the CSS variable which corresponds
78
- // with the 'name' property of the HTML element that triggered this function.
79
- document . documentElement . style . setProperty ( `--${ this . name } ` , this . value + suffix )
80
- }
81
-
82
- // Attach event listeners to each input
83
- inputs . forEach ( input => input . addEventListener ( 'change' , handleUpdate ) ) ;
84
- inputs . forEach ( input => input . addEventListener ( 'mousemove' , handleUpdate ) ) ;
66
+ ( ( ) => {
67
+ 'use strict' ;
68
+ // Declare & define variable reference to all inputs inside the 'control' class
69
+ const inputs = document . querySelectorAll ( '.controls input' ) ;
70
+
71
+ // Event Handler function. Such object destructure much default parameters wow
72
+ const handleUpdate = ( { target : { name, value, dataset : { sizing : suffix = '' } } } ) => {
73
+ document . documentElement . style . setProperty ( `--${ name } ` , value + suffix )
74
+ }
75
+
76
+ // Helper function to add/remove event listeners because the standard syntax is long and ugly
77
+ const listen = ( event , element , handler , cb = false , add = true ) => (
78
+ add ?
79
+ cb ?
80
+ ( ) => element . addEventListener ( event , handler ) :
81
+ element . addEventListener ( event , handler ) :
82
+ cb ?
83
+ ( ) => element . removeEventListener ( event , handler ) :
84
+ element . removeEventListener ( event , handler )
85
+ ) ;
86
+
87
+ // Attach event listeners to each input
88
+ inputs . forEach ( inp => {
89
+ // When input value is changed, call handleUpdate function
90
+ listen ( 'change' , inp , handleUpdate )
91
+ // When a mousedown event occurs on the input, attach *another* event listener to the input.
92
+ // This new event listener will listen for 'mousemove' event, and call on the
93
+ // handleUpdate function.
94
+ listen ( 'mousedown' , inp , listen ( 'mousemove' , inp , handleUpdate , true ) )
95
+ // When a mouseup event occurs on the input, remove the event listener for the 'mousemove'
96
+ // event (if one exists) that uses the `handleUpdate` function for the event handler.
97
+ listen ( 'mouseup' , inp , listen ( 'mousemove' , inp , handleUpdate , true , false ) )
98
+ } ) ;
99
+ } ) ( ) ;
85
100
</ script >
86
-
87
101
</ body >
88
-
89
- </ html >
102
+ </ html >
0 commit comments