-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
29 lines (26 loc) · 834 Bytes
/
main.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
const listaDeTeclas = document.querySelectorAll('.tecla');
function tocaSom(idAudio){
const elemento = document.querySelector(idAudio);
if (elemento && elemento.localName === 'audio'){
elemento.play();
}
else {
console.log('Inválido/ Não encontrado');
}
}
for(let contador = 0; contador < listaDeTeclas.length; contador++){
const tecla = listaDeTeclas[contador];
const instrumento = tecla.classList[1];
const idAudio = `#som_${instrumento}`;
tecla.onclick = function(){
tocaSom(idAudio);
}
tecla.onkeydown = function (evento) {
if(evento.code ==='space' || evento.code === 'Enter'){
tecla.classList.add('ativa');
}
}
tecla.onkeyup = function () {
tecla.classList.remove('ativa');
}
}