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
25 lines (20 loc) · 681 Bytes
/
index.js
File metadata and controls
25 lines (20 loc) · 681 Bytes
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
const ks = [...document.querySelectorAll('.key')];
// add event to k
ks.forEach((k) => {
k.addEventListener('transitionend', (e) => {
if (e.propertyName !== 'transform') return;
e.target.classList.remove('playing');
console.log(e);
});
});
window.addEventListener('keydown', (e) => playSound(e.keyCode));
function playSound(code) {
const btn = document.querySelector(`div[data-key='${code}']`);
const audio = document.querySelector(`audio[data-key='${code}']`);
if (!audio) return;
btn.classList.add('playing');
audio.currentTime = 0;
audio.play();
// setTimeout(() => btn.classList.remove('playing'), 500);
// console.log(code, btn, audio);
}