Visitar URL original
DONE :beer: · wesbos/JavaScript30@5c63632 · GitHub
Skip to content

Commit 5c63632

Browse files
committed
DONE 🍺
1 parent 5029405 commit 5c63632

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

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

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

6060
<script>
61+
function removeTransition (e) {
62+
if (e.propertyName !== 'transform') return;
63+
e.target.classList.remove('playing');
64+
}
6165

66+
function playSound (e) {
67+
const audio = document.querySelector('audio[data-key="' + e.keyCode + '"]');
68+
const key = document.querySelector('.key[data-key="' + e.keyCode + '"]');
69+
70+
if (!audio) return;
71+
audio.currentTime = 0;
72+
audio.play();
73+
74+
key.classList.add('playing');
75+
}
76+
77+
const keys = document.querySelectorAll('.key');
78+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
79+
window.addEventListener('keydown', playSound);
6280
</script>
6381

6482

0 commit comments

Comments
 (0)