Skip to content

Commit 287d4a4

Browse files
committed
finish js 01
1 parent 63775de commit 287d4a4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

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

6060
<script>
61+
function playSound(e) {
62+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
63+
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
6164

65+
if (!audio) return;
66+
audio.currentTime = 0; // rewind to the start
67+
audio.play();
68+
key.classList.add('playing');
69+
}
70+
71+
function removeTransition(e) {
72+
if (e.propertyName !== 'transform') return;
73+
this.classList.remove('playing');
74+
}
75+
const keys = Array.from(document.querySelectorAll('.key'));
76+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
77+
window.addEventListener('keydown', playSound);
6278
</script>
6379

6480

0 commit comments

Comments
 (0)