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 5959
6060< script >
6161
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+
6283</ script >
6384
6485
Original file line number Diff line number Diff line change 4141</ style >
4242
4343< button > </ button >
44+
4445< 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+ } ) ) ;
4557
4658</ script >
59+
4760</ body >
61+
4862</ html >
You can’t perform that action at this time.
0 commit comments