Skip to content

Commit 153cf2a

Browse files
committed
Day 1 done!!
1 parent ce2c7e6 commit 153cf2a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,26 @@
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) // stop the function from running
65+
return;
66+
audio.currentTime = 0; // to rewind it to the start
67+
audio.play();
68+
key.classList.add('playing'); // add, remove and toggle also available.
69+
}
6170

71+
72+
function removeTransition(e)
73+
{
74+
if(e.propertyName !== 'transform') return; //strip it if it's not a transform
75+
this.classList.remove('playing'); // this is the key here (opp of what is called)
76+
}
77+
78+
const keys = document.querySelectorAll('.key');
79+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
80+
window.addEventListener('keydown', playSound);
6281
</script>
6382

6483

0 commit comments

Comments
 (0)