-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
26 lines (21 loc) · 836 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
window.addEventListener("keydown", (e) => {
let audio = document.querySelector(`audio[data-key="${e.keyCode}"]`)
if (!audio) {return}
audio.currentTime = 0;
audio.play()
let key = document.querySelector(`.key[data-key="${e.keyCode}"]`)
key.classList.add("playing")
})
function removeTransition() {
this.classList.remove("playing")
}
const keys = document.querySelectorAll(".key");
keys.forEach(key => key.addEventListener("transitionend", removeTransition))
keys.forEach(div => div.addEventListener("click", () => {
let audio = document.querySelector(`audio[data-key="${div.getAttribute("data-key")}"]`)
if (!audio) {return}
audio.currentTime = 0;
audio.play()
let key = document.querySelector(`.key[data-key="${div.getAttribute("data-key")}"]`)
key.classList.add("playing")
}))