-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
40 lines (30 loc) · 918 Bytes
/
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
35
36
37
38
39
40
const allKeys = document.querySelectorAll(".key");
const sounds = [{ letter: "A", soundSrc: "assets/sounds/clap.wav" }];
function playAudio(key) {
const audio = document.querySelector(`audio[data-key="${key}"]`);
if (!audio) return;
audio.currentTime = 0;
audio.play();
}
function enableVisualEffect(key) {
const element = document.querySelector(`div[data-key="${key}"`);
if (!element) return;
element.classList.add("playing");
}
window.addEventListener("keydown", (e) => {
const key = e.key.toUpperCase();
playAudio(key);
enableVisualEffect(key);
});
window.addEventListener("touchstart", (e) => {
const key =
e.target.parentElement.dataset.key?.toUpperCase() ||
e.target.dataset.key?.toUpperCase();
playAudio(key);
enableVisualEffect(key);
});
allKeys.forEach((key) =>
key.addEventListener("transitionend", (event) =>
event.target.classList.remove("playing")
)
);