Visitar URL original
hook up drum kit · wesbos/JavaScript30@c14db38 · GitHub
Skip to content

Commit c14db38

Browse files
author
Elise Kain
committed
hook up drum kit
1 parent fe83039 commit c14db38

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@
5858
<audio data-key="76" src="sounds/tink.wav"></audio>
5959

6060
<script>
61+
62+
function playKey(e) {
63+
const playedKey = document.querySelector(`div[data-key="${e.keyCode}"]`);
64+
if (!playedKey) return;
65+
66+
// Add 'playing' class
67+
playedKey.classList.add('playing');
68+
69+
// Play corresponding audio
70+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
71+
audio.currentTime = 0;
72+
audio.play();
73+
};
74+
75+
function removeTransition(e) {
76+
if (e.propertyName !== 'transform') return;
77+
// Remove 'playing' class
78+
this.classList.remove('playing');
79+
}
80+
81+
// Event Listeners
82+
const keys = document.querySelectorAll('.key');
83+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
84+
window.addEventListener('keydown', playKey);
6185

6286
</script>
6387

0 commit comments

Comments
 (0)