forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
23 lines (18 loc) · 620 Bytes
/
main.js
File metadata and controls
23 lines (18 loc) · 620 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const keys = document.querySelectorAll('.keys');
keys.forEach(key => { key.addEventListener('transitionend', removeTransition)});
this.addEventListener('keydown', playSound);
// play the sound
function playSound(e) {
const keys = document.querySelector(`.key[data-key="${e.keyCode}"]`);
const audio = document.querySelectorAll(`audio[data-key="${e.keyCode}"]`)[0];
if (keys) {
const play = new Audio(audio.src);
play.play();
keys.classList.add('playing')
}
}
function removeTransition (e) {
if(e.propertyName === 'transform'){
e.target.className = 'key';
}
}