-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
32 lines (25 loc) · 959 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
26
27
28
29
30
31
32
const playSound = e => {
let keyCode;
if (e.type === 'keydown') {
keyCode = e.keyCode;
} else {
keyCode = e.target.getAttribute('data-key') || e.target.parentNode.getAttribute('data-key');
}
const audio = document.querySelector(`audio[data-key="${keyCode}"]`);
const key = document.querySelector(`div[data-key="${keyCode}"]`);
if (!audio) return;
key.classList.add('playing');
audio.currentTime = 0;
audio.play();
}
const removeTransition = e => {
if (e.propertyName !== 'transform') return;
e.target.classList.remove('playing');
}
const keys = Array.from(document.querySelectorAll('.key'));
keys.forEach(key => {
key.addEventListener('transitionend', removeTransition)
});
window.addEventListener('keydown', playSound);
window.addEventListener('touchstart', playSound);
window.addEventListener('click', playSound);