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+
65+ if ( ! audio ) return ; //stop the function altogether
66+
67+ key . classList . add ( 'playing' ) ;
68+ audio . currentTime = 0 ; // rewind to the start
69+ audio . play ( ) ;
70+
71+ }
72+
73+ function removeTransition ( e ) {
74+ if ( e . propertyName !== 'transform' ) return ;
75+ e . target . classList . remove ( 'playing' ) ;
76+ }
77+
78+ const keys = document . querySelectorAll ( '.key' ) ;
79+ keys . forEach ( key => key . addEventListener ( 'transitionend' , removeTransition ) ) ;
80+
81+ window . addEventListener ( 'keydown' , playSound ) ;
6182
6283</ script >
6384
6485
6586</ body >
6687</ html >
88+
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ body,html {
2323 margin : 1rem ;
2424 font-size : 1.5rem ;
2525 padding : 1rem .5rem ;
26- transition : all .07 s ease;
26+ transition : all .15 s ease;
2727 width : 10rem ;
2828 text-align : center;
2929 color : white;
@@ -32,8 +32,8 @@ body,html {
3232}
3333
3434.playing {
35- transform : scale (1.1 );
36- border-color : # ffc600 ;
35+ transform : scale (1.2 );
36+ border-color : # ccc600 ;
3737 box-shadow : 0 0 1rem # ffc600 ;
3838}
3939
You can’t perform that action at this time.
0 commit comments