Skip to content

Commit 7c9031b

Browse files
committed
Finished day 1
1 parent 38a754c commit 7c9031b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

01 - JavaScript Drum Kit/index-START.html renamed to 01 - JavaScript Drum Kit/index-completed.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@
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}"]`)
64+
if (!audio) {
65+
console.log("Sorry, that's not a valid key")
66+
return;
67+
}
68+
key.classList.add('playing')
69+
audio.currentTime = 0
70+
audio.play()
71+
}
72+
73+
function removeTransition(e) {
74+
if(e.propertyName !== 'transform') return;
75+
this.classList.remove('playing')
76+
}
77+
78+
const keys = document.querySelectorAll('.key')
79+
keys.forEach(key => key.addEventListener('transitionend', removeTransition))
80+
81+
window.addEventListener('keydown', playSound)
6182

6283
</script>
6384

0 commit comments

Comments
 (0)