File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change 5858 < audio data-key ="76 " src ="sounds/tink.wav "> </ audio >
5959
6060< 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 ) // stop the function from running
65+ return ;
66+ audio . currentTime = 0 ; // to rewind it to the start
67+ audio . play ( ) ;
68+ key . classList . add ( 'playing' ) ; // add, remove and toggle also available.
69+ }
6170
71+
72+ function removeTransition ( e )
73+ {
74+ if ( e . propertyName !== 'transform' ) return ; //strip it if it's not a transform
75+ this . classList . remove ( 'playing' ) ; // this is the key here (opp of what is called)
76+ }
77+
78+ const keys = document . querySelectorAll ( '.key' ) ;
79+ keys . forEach ( key => key . addEventListener ( 'transitionend' , removeTransition ) ) ;
80+ window . addEventListener ( 'keydown' , playSound ) ;
6281</ script >
6382
6483
You can’t perform that action at this time.
0 commit comments