-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
31 lines (29 loc) · 896 Bytes
/
main.js
File metadata and controls
31 lines (29 loc) · 896 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
30
31
function tocaSom(seletorAudio) {
const elemento = document.querySelector(seletorAudio);
if (elemento != null && elemento.localName === "audio") {
elemento.play();
} else {
// alert("Elemento não encontrado");
console.log("Elemento não encontrado ou seletor inválido");
}
}
const listaDeTeclas = document.querySelectorAll(".tecla");
//Para
for (let i = 0; i < listaDeTeclas.length; i++) {
const tecla = listaDeTeclas[i];
const instrumento = tecla.classList[1];
const idAudio = `#som_${instrumento}`;
tecla.onclick = function () {
tocaSom(idAudio);
};
//Add evento no teclado com o onkeydown
tecla.onkeydown = function (evento) {
if (evento.code === "Space" || evento.code === "Enter") {
tecla.classList.add("ativa");
}
};
//Add evento no teclado com o onkeyup
tecla.onkeyup = function () {
tecla.classList.remove("ativa");
};
}