File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Expand file tree Collapse file tree 1 file changed +17
-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+ if ( ! audio ) return ; //this means if no sound to play it exits the function
66+ audio . currentTime = 0 ; //this is so you can hit the key over and over without delay
67+ audio . play ( ) ;
68+ key . classList . add ( 'playing' ) ;
69+ } ;
70+
71+ function removeTransition ( e ) {
72+ if ( e . propertyName !== 'transform' ) return ; //skip it
73+ this . classList . remove ( 'playing' ) ;
74+ }
75+
76+ const keys = document . querySelectorAll ( '.key' ) ;
77+ keys . forEach ( key => key . addEventListener ( 'transitionend' , removeTransition ) ) ;
78+ window . addEventListener ( 'keydown' , playSound ) ;
6279</ script >
6380
6481
You can’t perform that action at this time.
0 commit comments