Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 27098aa

Browse files
committed
adding script wesbos#1
1 parent be63f4d commit 27098aa

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,24 @@
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) return; // stop the function from running all together
65+
audio.currentTime = 0; // restarts the sound
66+
audio.play();
67+
key.classList.add('playing');
68+
}
6169

62-
</script>
70+
function removeTransition(e) {
71+
if(e.propertyName !== 'transform') return; // skip it if its not a transform;
72+
this.classList.remove('playing');
73+
}
6374

75+
const keys = document.querySelectorAll('.key');
76+
keys.forEach(key => key.addEventListener('transitionend',removeTransition));
77+
window.addEventListener('keydown',playSound);
78+
</script>
6479

6580
</body>
6681
</html>

0 commit comments

Comments
 (0)