Visitar URL original
Built a drum kit · wesbos/JavaScript30@a339c6d · GitHub
Skip to content

Commit a339c6d

Browse files
committed
Built a drum kit
1 parent 61b13da commit a339c6d

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

‎01 - JavaScript Drum Kit/index-START.html‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,22 @@
5858
<audio data-key="76" src="sounds/tink.wav"></audio>
5959

6060
<script>
61+
const buttons = Array.from(document.querySelectorAll('.key'));
62+
const sounds = Array.from(document.querySelectorAll('audio'));
6163

62-
</script>
64+
buttons.forEach(button => {
65+
button.addEventListener('transitionend', event => event.target.classList.remove('playing'))
66+
});
67+
68+
document.addEventListener('keypress', event => {
69+
let button = buttons.filter(button => event.keyCode == button.dataset.key).pop();
70+
let sound = sounds.filter(sound => event.keyCode == sound.dataset.key).pop();
6371

72+
button.classList.add('playing');
73+
sound.currentTime = 0;
74+
sound.play();
75+
});
76+
</script>
6477

6578
</body>
6679
</html>

0 commit comments

Comments
 (0)