-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
41 lines (30 loc) · 1.07 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
const pianokeys = document.querySelectorAll('.piano-keys .key'),
volumeslider = document.querySelector('.volume-slider input'),
keyscheckbox = document.querySelector('.keys-checkbox input');
let allkeys = [],
audio = new Audio('a.wav')
const playtune = (key) => {
audio.src = `${key}.wav`
audio.play();
const clickedkey = document.querySelector(`[data-key = "${key}"]`);
clickedkey.classList.add("active");
setTimeout(() => {
clickedkey.classList.remove("active");
}, 150)
}
pianokeys.forEach(key => {
allkeys.push(key.dataset.key)
key.addEventListener('click', () => playtune(key.dataset.key));
});
const handlevolume = (e) => {
audio.volume = e.target.value
}
const showhidekeys = () => {
pianokeys.forEach(key => key.classList.toggle("hide"));
}
const pressedkey = (e) => {
if (allkeys.includes(e.key)) playtune(e.key)
}
document.addEventListener("keydown", pressedkey);
volumeslider.addEventListener("input", handlevolume);
keyscheckbox.addEventListener("click", showhidekeys);