-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
29 lines (25 loc) · 852 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
29
document.addEventListener("DOMContentLoaded", function() {
console.log('Man can never be hot!');
const keys = document.querySelectorAll('.key');
const removeTransition = function(e) {
if (e.propertyName != 'transform') return;
this.classList.remove('playing');
};
const playSound = function(code) {
const audio = document.querySelector(`audio[data-key="${code}"]`);
const key = document.querySelector(`.key[data-key="${code}"]`);
if (!audio) return;
audio.currentTime = 0;
audio.play();
key.classList.add('playing');
};
window.addEventListener('keydown', function(e) {
playSound(e.keyCode);
});
keys.forEach(key => {
key.addEventListener('click', function() {
playSound(this.attributes['data-key'].value);
});
key.addEventListener('transitionend', removeTransition)
});
});