Skip to content

Commit 11c61fd

Browse files
committed
Update index-START.html
Added solution for day 1.
1 parent ce2c7e6 commit 11c61fd

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

01 - JavaScript Drum Kit/index-START.html

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

6060
<script>
61+
window.addEventListener('keydown', function(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;
65+
audio.currentTime = 0; // rewind to the start
66+
audio.play();
67+
key.classList.add('playing');
68+
});
6169

70+
function removeTransition(e) {
71+
if (e.propertyName !== 'transform') return;
72+
this.classList.remove('playing');
73+
}
74+
75+
const keys = document.querySelectorAll('.key');
76+
keys.forEach(key => {
77+
key.addEventListener('transitionend', removeTransition);
78+
});
6279
</script>
6380

6481

0 commit comments

Comments
 (0)