Skip to content

Commit a8fa888

Browse files
committed
day 1
1 parent 61b13da commit a8fa888

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@
5959

6060
<script>
6161

62+
function playSound(e){
63+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"`)
64+
const currKey = document.querySelector(`.key[data-key="${e.keyCode}"`)
65+
66+
if (!audio) return;
67+
68+
audio.currentTime = 0; //rewind to start allowing repeated clicks
69+
audio.play();
70+
currKey.classList.add('playing');
71+
}
72+
73+
function removeTransition(e){
74+
if(e.propertyName !== "transform") return;
75+
this.classList.remove('playing');
76+
}
77+
78+
79+
(() => {
80+
document.addEventListener('keydown', playSound);
81+
const keys = document.querySelectorAll('.key');
82+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
83+
})();
6284
</script>
6385

6486

0 commit comments

Comments
 (0)