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
+ function playSound ( e ) {
62
+ const audio = document . querySelector ( `audio[data-key="${ e . keyCode } "]` )
63
+ const key = document . querySelector ( `.key[data-key="${ e . keyCode } "]` )
64
+ if ( ! audio ) {
65
+ console . log ( "Sorry, that's not a valid key" )
66
+ return ;
67
+ }
68
+ key . classList . add ( 'playing' )
69
+ audio . currentTime = 0
70
+ audio . play ( )
71
+ }
72
+
73
+ function removeTransition ( e ) {
74
+ if ( e . propertyName !== 'transform' ) return ;
75
+ this . classList . remove ( 'playing' )
76
+ }
77
+
78
+ const keys = document . querySelectorAll ( '.key' )
79
+ keys . forEach ( key => key . addEventListener ( 'transitionend' , removeTransition ) )
80
+
81
+ window . addEventListener ( 'keydown' , playSound )
61
82
62
83
</ script >
63
84
You can’t perform that action at this time.
0 commit comments