Skip to content

Commit 7539c5c

Browse files
author
Artur Charaev
committed
day 1
1 parent 2516cfb commit 7539c5c

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

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 { keyCode } = e;
63+
const audio = document.querySelector(`audio[data-key="${keyCode}"]`);
64+
if (!audio) return;
65+
const key = document.querySelector(`.key[data-key="${keyCode}"]`);
66+
audio.currentTime = 0;
67+
audio.play();
68+
key.classList.add('playing');
69+
}
70+
function removeTransition(e) {
71+
if (e.propertyName !== 'transform') return;
72+
this.classList.remove('playing');
73+
}
6174

75+
const keys = 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)