File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 5959
6060< script >
6161
62+ const playSound = ( e ) => {
63+ const audio = document . querySelector ( `audio[data-key="${ e . keyCode } "]` ) ;
64+ const key = document . querySelector ( `.key[data-key="${ e . keyCode } "]` ) ;
65+
66+ if ( ! audio ) return ; // stop the function from running if that key has no audio
67+ audio . currentTime = 0 ; // rewind to the start of the audio
68+ audio . play ( ) ;
69+ key . classList . add ( 'playing' ) ;
70+ }
71+
72+ function removeTransition ( e ) {
73+ if ( e . propertyName !== 'transform' ) return ; // skip it if it's not a transform
74+ // console.log(this); // nice cheater way to figure out what 'this' is
75+ this . classList . remove ( 'playing' ) ;
76+ }
77+
78+ const keys = document . querySelectorAll ( '.key' ) ;
79+
80+ keys . forEach ( key => key . addEventListener ( 'transitionend' , removeTransition ) ) ;
81+
82+ window . addEventListener ( 'keydown' , playSound ) ;
83+
6284</ script >
6385
6486
You can’t perform that action at this time.
0 commit comments