Skip to content
This repository was archived by the owner on Feb 13, 2022. It is now read-only.

Commit 5c16b1f

Browse files
committed
solution wesbos#1
1 parent 38a754c commit 5c16b1f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

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

6060
<script>
61+
const keys = document.querySelectorAll('.key');
6162

63+
function playSound(evt) {
64+
const key = document.querySelector(`.key[data-key="${evt.keyCode}"]`);
65+
const audio = document.querySelector(`audio[data-key="${evt.keyCode}"]`);
66+
67+
if (!audio) return;
68+
69+
key.classList.add('playing');
70+
71+
audio.currentTime = 0;
72+
audio.play();
73+
}
74+
75+
function removeTransition(evt) {
76+
if (evt.propertyName !== 'transform') return;
77+
78+
this.classList.remove('playing');
79+
}
80+
81+
document.addEventListener('keydown', playSound);
82+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
6283
</script>
6384

6485

0 commit comments

Comments
 (0)