-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
34 lines (28 loc) · 1.06 KB
/
script.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
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}"]`);
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
if (!audio) return;
key.classList.add("playing");
audio.currentTime = 0;
audio.play();
}
const keys = Array.from(document.querySelectorAll(".key"));
keys.forEach((key) => key.addEventListener("transitionend", removeTransition));
window.addEventListener("keydown", playSound);
const drumkit = document.querySelector(".keys");
function playDrum(event) {
if (event.target.classList.contains("audio")) {
event.preventDefault();
const audio = document.querySelector(`audio[data-key="${event.target.dataset.key}"]`);
const key = document.querySelector(`.key[data-key="${event.target.dataset.key}"]`);
if (!audio) return;
key.classList.add("playing");
audio.currentTime = 0;
audio.play();
}
}
drumkit.addEventListener("click", playDrum);