File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree 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 ) return ; // do nothing if there's no audio element associated with key.
65+ audio . currentTime = 0 ; // rewind audio element to the beginning.
66+ audio . play ( ) ; // play audio element associated with key.
67+ key . classList . add ( 'playing' ) ;
68+ }
6169
70+ function removeTransition ( e ) {
71+ if ( e . propertyName !== 'transform' ) return ; // skip property if it is not transform
72+ this . classList . remove ( 'playing' ) ;
73+ }
74+
75+ const keys = document . querySelectorAll ( '.key' ) ;
76+ keys . forEach ( key => key . addEventListener ( 'transitionend' , removeTransition ) ) ;
77+ window . addEventListener ( 'keydown' , playSound ) ;
6278</ script >
6379
6480
You can’t perform that action at this time.
0 commit comments