Skip to content

Commit c830e5f

Browse files
committed
Finished Exercise 1
1 parent 86bade4 commit c830e5f

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

01 - JavaScript Drum Kit/index-START.html

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

6060
<script>
61-
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) {
65+
return;
66+
}
67+
audio.currentTime = 0;
68+
audio.play();
69+
// console.log(key);
70+
key.classList.add('playing');
71+
}
72+
function removeTransition(e) {
73+
if (e.propertyName !== 'transform') {
74+
return;
75+
}
76+
console.log(e.propertyName);
77+
this.classList.remove('playing');
78+
}
79+
const keys = document.querySelectorAll('.key');
80+
keys.forEach(key => {
81+
key.addEventListener('transitionend', removeTransition);
82+
});
83+
window.addEventListener('keydown', playSound);
6284
</script>
6385

6486

0 commit comments

Comments
 (0)