-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
37 lines (31 loc) · 1.16 KB
/
app.js
File metadata and controls
37 lines (31 loc) · 1.16 KB
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
30
31
32
33
34
// remove the playing animation
function removeTransition(e) {
if (e.propertyName !== 'transform') return;
e.target.classList.remove('playing');
}
// play sound when pressing keyboard keys
function playOnKeyboard(e) {
e.stopPropagation();
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
const key = document.querySelector(`div[data-key="${e.keyCode}"]`);
if (!audio) return;
key.classList.add('playing');
audio.currentTime = 0;
audio.play();
}
// play sound when clicking on buttons
function playOnClick(e) {
e.stopPropagation();
const dataCode = parseInt(e.target.dataset.key);
const audio = document.querySelector(`audio[data-key="${dataCode}"]`);
const key = document.querySelector(`div[data-key="${dataCode}"]`);
if (!audio) return;
key.classList.add('playing');
audio.currentTime = 0;
audio.play();
}
//Event listeners
const buttons = document.querySelectorAll('.button');
window.addEventListener('keydown', playOnKeyboard);
buttons.forEach(button => button.addEventListener('transitionend', removeTransition));
buttons.forEach(button => button.addEventListener('click', playOnClick));