-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
41 lines (32 loc) · 1.08 KB
/
app.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
(function() {
// code for allowing clicks
const items = document.querySelectorAll(".item");
items.forEach(item =>
item.addEventListener("click", function(e) {
const key = item.dataset.key;
const audio = document.querySelector(`audio[data-key="${key}"`);
if (!audio) return;
audio.currentTime = 0;
audio.play();
item.classList.add("playing");
item.addEventListener("transitionend", removeTransition);
})
);
function removeTransition(e) {
if (e.propertyName !== "transform") return;
this.classList.remove("playing");
}
function playSoundOnKeyPress(e) {
const audio = document.querySelector(`audio[data-key="${e.keyCode}"`);
const item = document.querySelector(`.item[data-key="${e.keyCode}"]`);
if (!audio) return;
audio.currentTime = 0;
audio.play();
item.classList.add("playing");
const items = document.querySelectorAll(".item");
items.forEach(item =>
item.addEventListener("transitionend", removeTransition)
);
}
window.addEventListener("keydown", playSoundOnKeyPress);
})();