File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 5959
6060< script >
6161
62+ function playSound ( event ) {
63+ /* // ES5 version
64+ const audioKeyCode = event.keyCode;
65+ const audio = document.querySelector("audio[data-key='"+audioKeyCode+"']");
66+ */
67+ const audio = document . querySelector ( `audio[data-key="${ event . keyCode } "]` ) ;
68+ const key = document . querySelector ( `.key[data-key="${ event . keyCode } "]` ) ;
69+ if ( ! audio ) return ; // stop running function all together
70+ audio . currentTime = 0 ; // resets or rewinds to the start
71+ audio . play ( ) ;
72+ key . classList . add ( "playing" ) ;
73+ } ;
74+
75+ function removeTransition ( e ) {
76+ if ( e . propertyName !== "transform" ) return ; //skip it if it's not a transform
77+ this . classList . remove ( "playing" ) ;
78+ }
79+
80+ const keys = document . querySelectorAll ( ".key" ) ;
81+ keys . forEach ( key => key . addEventListener ( "transitionend" , removeTransition ) ) ;
82+
83+
84+ window . addEventListener ( "keydown" , playSound ) ;
85+
86+
87+
6288</ script >
6389
6490
You can’t perform that action at this time.
0 commit comments