Skip to content

Commit 7409a1a

Browse files
Madison DicksonMadison Dickson
authored andcommitted
commit first project
1 parent be63f4d commit 7409a1a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

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

6060
<script>
6161

62+
function playAudio(keyCode){
63+
const audio = document.querySelector(`audio[data-key="${keyCode}"]`);
64+
const key = document.querySelector(`.key[data-key="${keyCode}"]`);
65+
if(audio){
66+
key.classList.add('playing');
67+
audio.currentTime = 0;
68+
audio.play();
69+
}
70+
}
71+
72+
let keys = document.querySelectorAll('.key');
73+
keys.forEach(key => {
74+
key.addEventListener('transitionend', function(e) {
75+
if(e.propertyName == 'transform')
76+
this.classList.remove('playing');
77+
})
78+
key.addEventListener('click', function(e) {
79+
playAudio(this.dataset.key)
80+
})
81+
})
82+
window.addEventListener('keydown', e => {
83+
playAudio(e.keyCode)
84+
});
6285
</script>
6386

6487

0 commit comments

Comments
 (0)