Skip to content

Commit ad9c58b

Browse files
committed
drum kit
1 parent fe83039 commit ad9c58b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,24 @@
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+
key.classList.add('playing');
72+
audio.currentTime = 0;
73+
audio.play();
74+
}
75+
76+
const keys = Array.from(document.querySelectorAll('.key'));
77+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
78+
window.addEventListener( 'keydown', playSound);
6279
</script>
6380

6481

0 commit comments

Comments
 (0)