forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
28 lines (22 loc) · 823 Bytes
/
scripts.js
File metadata and controls
28 lines (22 loc) · 823 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
/********************************************
* Call functions on Window Event Listener
********************************************/
function playSound(e) {
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
// stop the function from running all together
if (!audio) return;
// Rewinds audio file to start
audio.currentTime = 0;
// Plays audio file
audio.play();
key.classList.add('playing');
}
function removeTransition(e) {
// skip if it's not a transform
if(e.propertyName !== 'transform') return;
this.classList.remove('playing');
}
const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
window.addEventListener('keydown', playSound);