Skip to content

Commit 082eb1b

Browse files
committed
Completed drum exercise.
1 parent 38a754c commit 082eb1b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@
5959

6060
<script>
6161

62+
const playSound = (e) => {
63+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
64+
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
65+
66+
if (!audio) return; // stop the function from running if that key has no audio
67+
audio.currentTime = 0; // rewind to the start of the audio
68+
audio.play();
69+
key.classList.add('playing');
70+
}
71+
72+
function removeTransition(e) {
73+
if (e.propertyName !== 'transform') return; // skip it if it's not a transform
74+
// console.log(this); // nice cheater way to figure out what 'this' is
75+
this.classList.remove('playing');
76+
}
77+
78+
const keys = document.querySelectorAll('.key');
79+
80+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
81+
82+
window.addEventListener('keydown', playSound);
83+
6284
</script>
6385

6486

0 commit comments

Comments
 (0)