Skip to content

Commit d677505

Browse files
committed
Update index-START.html
Practice wesbos#1 for TOP's DOM Manipulation
1 parent 09e3480 commit d677505

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

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

6060
<script>
61+
62+
63+
64+
function playSound(e){
65+
const audio = document.querySelector(`audio[data-key = "${e.keyCode}"]`);
66+
const key = document.querySelector(`.key[data-key = "${e.keyCode}"]`);
67+
if(!audio) return; // stops the function from running altogether
68+
audio.currentTime = 0;
69+
audio.play();
70+
key.classList.add('playing');
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+
window.addEventListener('keydown', playSound);
6181

6282
</script>
6383

0 commit comments

Comments
 (0)