forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
23 lines (20 loc) · 697 Bytes
/
index.js
File metadata and controls
23 lines (20 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
window.addEventListener("load", () => {
const keys = Array.from(document.getElementsByClassName("key"));
const audio = Array.from(document.getElementsByTagName("audio"));
document.addEventListener("keydown", e => {
const keyCode = String(e.keyCode);
const key = keys.find(key => key.dataset.key === keyCode);
if (key) {
const sound = audio.find(sound => sound.dataset.key === keyCode);
key.classList.add("playing");
sound.play();
}
});
document.addEventListener("keyup", e => {
const keyCode = String(e.keyCode);
const key = keys.find(key => key.dataset.key === keyCode);
if (key) {
key.classList.remove("playing");
}
});
});