forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
20 lines (16 loc) · 670 Bytes
/
app.js
File metadata and controls
20 lines (16 loc) · 670 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function playSound(e) {
const audio = document.querySelector(`audio[data-key= "${e.keyCode}"]`);
const key = document.querySelector(`.key[data-key= "${e.keyCode}"]`);
console.log(audio);
if (!audio) return; // stop the function from running
audio.currentTime = 0; // rewind to the start
audio.play();
key.classList.add('playing');
}
function removeTransition(e) {
if(e.propertyName !== 'transform') return; // skip if it's not a transform
this.classList.remove('playing');
}
const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
window.addEventListener('keydown', playSound);