-
Notifications
You must be signed in to change notification settings - Fork 41.8k
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (24 loc) · 779 Bytes
/
script.js
File metadata and controls
31 lines (24 loc) · 779 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
26
27
28
29
30
31
function removeTransition(e) {
if(e.propertyName != 'transform') return;
this.classList.remove('playing');
}
function playSoundByKeyCode(keyCode) {
const audio = document.querySelector(`audio[data-key="${keyCode}"]`);
const key = document.querySelector(`.key[data-key="${keyCode}"]`);
if(!audio) return;
audio.currentTime = 0;
audio.play();
key.classList.add('playing');
}
function playSound(e) {
playSoundByKeyCode(e.keyCode);
}
const keys = Array.from(document.querySelectorAll('.key'));
keys.forEach(key => {
key.addEventListener('transitionend', removeTransition);
key.addEventListener('click', () => {
const keyCode = key.getAttribute('data-key');
playSoundByKeyCode(keyCode)
})
});
window.addEventListener('keydown', playSound);