File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 58
58
< audio data-key ="76 " src ="sounds/tink.wav "> </ audio >
59
59
60
60
< script >
61
+ document . addEventListener ( 'keyup' , play ) ;
62
+ document . querySelectorAll ( '.key' ) . forEach (
63
+ el => el . addEventListener ( 'transitionend' , removeTransition )
64
+ ) ;
61
65
66
+ function play ( event ) {
67
+ const key = event . keyCode ;
68
+ const button = document . querySelector ( `.key[data-key="${ key } "]` ) ;
69
+ const audio = document . querySelector ( `audio[data-key="${ key } "]` ) ;
70
+
71
+ if ( ! button ) { return ; }
72
+
73
+ button . classList . add ( 'playing' ) ;
74
+ audio . currentTime = 0 ;
75
+ audio . play ( ) ;
76
+ }
77
+
78
+ function removeTransition ( event ) {
79
+ if ( event . propertyName !== 'transform' ) { return ; }
80
+
81
+ event . target . classList . remove ( 'playing' ) ;
82
+ }
62
83
</ script >
63
84
64
85
You can’t perform that action at this time.
0 commit comments