-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
45 lines (35 loc) · 850 Bytes
/
main.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
const keys = Array.from(document.querySelectorAll('.key'));
const keyCode = {
A: 65,
S: 83,
D: 68,
F: 70,
G: 71,
H: 72,
J: 74,
K: 75,
L: 76,
};
function removeTransition(e) {
if (e.propertyName !== 'transform') return;
e.target.classList.remove('playing');
}
function playSound(e) {
const audio = document.querySelector(
`audio[data-key="${e.keyCode || keyCode[e.target.innerHTML]}"]`
);
const key = document.querySelector(
`div[data-key="${e.keyCode || keyCode[e.target.innerHTML]}"]`
);
if (!audio) return;
key.classList.add('playing');
audio.currentTime = 0;
audio.play();
}
keys.forEach((key) => key.addEventListener('transitionend', removeTransition));
window.addEventListener('keydown', playSound);
keys.forEach((key) =>
key.addEventListener('click', (e) => {
playSound(e);
})
);