Skip to content

Commit 6012f9f

Browse files
committed
Complete day1
1 parent 7c04fd5 commit 6012f9f

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
@@ -59,7 +59,23 @@
5959
<audio data-key="76" src="sounds/tink.wav"></audio>
6060

6161
<script>
62+
function playSound(e) {
63+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`)
64+
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`)
65+
if(!audio) return;
66+
audio.currentTime = 0;
67+
audio.play();
68+
key.classList.add('playing');
69+
}
6270

71+
function removeTransition(e){
72+
if(e.propertyName != 'transform') return;
73+
this.classList.remove('playing');
74+
}
75+
76+
const keys = document.querySelectorAll('.key')
77+
keys.forEach(key => key.addEventListener('transitionend', removeTransition))
78+
window.addEventListener('keydown', playSound);
6379
</script>
6480

6581

0 commit comments

Comments
 (0)