File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-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
+
62
+
63
+
64
+ function playSound ( e ) {
65
+ const audio = document . querySelector ( `audio[data-key = "${ e . keyCode } "]` ) ;
66
+ const key = document . querySelector ( `.key[data-key = "${ e . keyCode } "]` ) ;
67
+ if ( ! audio ) return ; // stops the function from running altogether
68
+ audio . currentTime = 0 ;
69
+ audio . play ( ) ;
70
+ key . classList . add ( 'playing' ) ;
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
+ window . addEventListener ( 'keydown' , playSound ) ;
61
81
62
82
</ script >
63
83
You can’t perform that action at this time.
0 commit comments