forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptEs6.js
More file actions
29 lines (24 loc) · 953 Bytes
/
scriptEs6.js
File metadata and controls
29 lines (24 loc) · 953 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
//=============================================
// javascript30 Version ES6
//=============================================
function removeTransition(e) {
//console.log(e);
if (e.propertyName !== 'transform') return; // Skip if it's not transformed
//console.log(e.propertyName);
this.classList.remove('playing');
};
function playSound(e) {
// body
//console.log(e.keyCode);
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
//console.log(audio);
if (!audio) return; // Stops the function from running all together;
const key = document.querySelector(`div[data-key="${e.keyCode}"]`);
//console.log(key);
audio.currentTime = 0; // Rewind to the start
audio.play();
key.classList.add('playing');
};
const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
window.addEventListener('keydown', playSound);