File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
25 - Event Capture, Propagation, Bubbling and Once Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 59
59
60
60
< script >
61
61
62
+ function playSound ( e ) {
63
+ const audio = document . querySelector ( `audio[data-key="${ e . keyCode } "]` ) ;
64
+ const key = document . querySelector ( `.key[data-key="${ e . keyCode } "]` ) ;
65
+
66
+ if ( ! audio ) return ;
67
+ audio . currentTime = 0 ;
68
+ audio . play ( ) ;
69
+ key . classList . add ( 'playing' ) ;
70
+ } ;
71
+
72
+ function removeTransition ( e ) {
73
+ if ( e . propertyName !== 'transform' ) return ;
74
+ this . classList . remove ( 'playing' ) ;
75
+ } ;
76
+
77
+ window . addEventListener ( 'keydown' , playSound ) ;
78
+
79
+ const keys = document . querySelectorAll ( '.key' ) ;
80
+ keys . forEach ( key => key . addEventListener ( 'transitionend' , removeTransition ) ) ; // animationend works the same way for animations
81
+
82
+
62
83
</ script >
63
84
64
85
Original file line number Diff line number Diff line change 41
41
</ style >
42
42
43
43
< button > </ button >
44
+
44
45
< script >
46
+ const divs = document . querySelectorAll ( 'div' ) ;
47
+
48
+ function logText ( e ) {
49
+ console . log ( this . classList . value ) ;
50
+ // e.stopPropagation();
51
+ } ;
52
+
53
+ divs . forEach ( div => div . addEventListener ( 'click' , logText , {
54
+ capture : false ,
55
+ // once: true // makes so it runs once and then won't run again until refreshed -- good for store checkouts
56
+ } ) ) ;
45
57
46
58
</ script >
59
+
47
60
</ body >
61
+
48
62
</ html >
You can’t perform that action at this time.
0 commit comments