File tree Expand file tree Collapse file tree 1 file changed +30
-3
lines changed Expand file tree Collapse file tree 1 file changed +30
-3
lines changed Original file line number Diff line number Diff line change 11<!DOCTYPE html>
22< html lang ="en ">
3+
34< head >
45 < meta charset ="UTF-8 ">
56 < title > JS Drum Kit</ title >
67 < link rel ="stylesheet " href ="style.css ">
78</ head >
9+
810< body >
911
1012
5759 < audio data-key ="75 " src ="sounds/tom.wav "> </ audio >
5860 < audio data-key ="76 " src ="sounds/tink.wav "> </ audio >
5961
60- < script >
62+ < script >
63+ const keys = document . querySelectorAll ( '.key' ) ;
64+ window . addEventListener ( 'keydown' , playSound ) ;
65+ keys . forEach ( key => key . addEventListener ( 'transitionend' , removeTransition ) ) ;
66+
67+ /* ===== functions below ====== */
68+ function playSound ( e ) {
69+ const audio = document . querySelector ( `audio[data-key="${ e . keyCode } "]` ) ;
70+ const key = document . querySelector ( `.key[data-key="${ e . keyCode } "]` ) ;
71+
72+ if ( ! audio ) return ;
6173
62- </ script >
74+ audio . currentTime = 0 ;
75+ audio . play ( ) ;
76+ key . classList . add ( 'playing' ) ;
77+
78+ // console.log('key:', key);
79+ // console.log('audio:', audio);
80+
81+ } ;
82+
83+ function removeTransition ( e ) {
84+ if ( e . propertyName !== 'transform' ) return ;
85+ this . classList . remove ( 'playing' ) ;
86+ // console.log('this : ', this);
87+ } ;
88+ </ script >
6389
6490
6591</ body >
66- </ html >
92+
93+ </ html >
You can’t perform that action at this time.
0 commit comments