File tree 3 files changed +26
-8
lines changed 3 files changed +26
-8
lines changed Original file line number Diff line number Diff line change 1
1
<!DOCTYPE html>
2
2
< html lang ="en ">
3
+
3
4
< head >
4
5
< meta charset ="UTF-8 ">
5
6
< title > JS Drum Kit</ title >
6
7
< link rel ="stylesheet " href ="style.css ">
7
8
</ head >
8
- < body >
9
9
10
+ < body >
10
11
11
12
< div class ="keys ">
12
13
< div data-key ="65 " class ="key ">
57
58
< audio data-key ="75 " src ="sounds/tom.wav "> </ audio >
58
59
< audio data-key ="76 " src ="sounds/tink.wav "> </ audio >
59
60
60
- < script >
61
-
62
- </ script >
63
-
64
-
61
+ < script src ="index.js "> </ script >
65
62
</ body >
66
- </ html >
63
+
64
+ </ html >
Original file line number Diff line number Diff line change
1
+ [ ...document . querySelectorAll ( '.key' ) ]
2
+ . forEach ( key => {
3
+ key . addEventListener ( 'transitionend' , ( e ) => {
4
+ // This event is called for each property that ended a transition.
5
+
6
+ key . classList . remove ( 'playing' ) ;
7
+ } ) ;
8
+ } ) ;
9
+
10
+ window . addEventListener ( 'keyup' , ( { keyCode } ) => {
11
+ const keyElement = document . querySelector ( `.key[data-key="${ keyCode } "]` ) ;
12
+ const audioElement = document . querySelector ( `audio[data-key="${ keyCode } "]` ) ;
13
+
14
+ if ( keyElement && audioElement ) {
15
+ audioElement . currentTime = 0 ; // rewind to start
16
+ audioElement . play ( ) ;
17
+
18
+ keyElement . classList . add ( 'playing' ) ;
19
+ }
20
+ } ) ;
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ body,html {
23
23
margin : 1rem ;
24
24
font-size : 1.5rem ;
25
25
padding : 1rem .5rem ;
26
- transition : all .07 s ease;
26
+ transition : all .15 s ease-in-out ;
27
27
width : 10rem ;
28
28
text-align : center;
29
29
color : white;
You can’t perform that action at this time.
0 commit comments