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