-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (50 loc) · 1.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// The e is short for event
// The simplest way to create an event is to click somewhere on the page.
// When you click, a click event is triggered. This event is actually an object containing information about the action that just happened.
const btn = document.querySelectorAll('.drum');
const clickListner = (word) => {
switch (word) {
case 'w':
let w = new Audio('sounds/tom-1.mp3');
w.play();
break;
case 'a':
let a = new Audio('sounds/tom-2.mp3');
a.play();
break;
case 's':
let s = new Audio('sounds/tom-3.mp3');
s.play();
break;
case 'd':
let d = new Audio('sounds/tom-4.mp3');
d.play();
break;
case 'j':
let j = new Audio('sounds/crash.mp3');
j.play();
break;
case 'k':
let k = new Audio('sounds/kick-bass.mp3');
k.play();
break;
case 'l':
let l = new Audio('sounds/snare.mp3');
l.play();
break;
default:
console.log('press right key');
}
};
// event will listen when the click happens
for (let i = 0; i < btn.length; i++) {
btn[i].addEventListener('click', (e) => {
let text = e.target.textContent;
clickListner(text);
});
}
// event occurs when certain key is pressed
document.addEventListener('keydown', (e) => {
let key = e.key;
clickListner(key);
});