-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
23 lines (18 loc) · 716 Bytes
/
script.js
File metadata and controls
23 lines (18 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* STORE ARRAY OF TARGETED KEYS IN A VARIABLE */
let keys = document.querySelectorAll(".key");
/* CHANGE STYLE OF THE KEY WHEN CLICKED */
keys.forEach((key) =>
key.addEventListener("click", () => {
key.classList.add("key-active");
setTimeout(() => {
key.classList.remove("key-active");
}, 50);
})
);
/* PLAY AUDIO */
keys.forEach(key => key.addEventListener("click", (e) => {
const audio = document.querySelector(`audio[data-key="${e.target.dataset.key}"]`)
if (!audio) return; // stop the function form running if e.target is does not have/find an audio
audio.currentTime = 0; // rewind audio to zero when finished or pressed again
audio.play();
}));