Visitar URL original
Finished day 01 - 🤘 · wesbos/JavaScript30@cffa1c3 · GitHub
Skip to content

Commit cffa1c3

Browse files
committed
Finished day 01 - 🤘
1 parent 86bade4 commit cffa1c3

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

‎01 - JavaScript Drum Kit/index-START.html‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,25 @@
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 running if the keyCode does not match
6165

66+
audio.currentTime = 0; // rewind to the start
67+
audio.play();
68+
key.classList.add('playing');
69+
}
70+
71+
function removeTransition(e) {
72+
if (e.propertyName !== 'transform') return; // skip if it's not a transform
73+
this.classList.remove('playing');
74+
}
75+
76+
const keys = document.querySelectorAll('.key');
77+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
78+
79+
window.addEventListener('keydown', playSound);
6280
</script>
6381

6482

0 commit comments

Comments
 (0)