Skip to content

Commit adbedc3

Browse files
Razvan HoreangaRazvan Horeanga
authored andcommitted
finished the functionality
1 parent 4a21f02 commit adbedc3

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,25 @@
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-
console.log(audio);
64-
});
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; //stops the function
65+
audio.currentTime = 0;//rewind to the beginning of the audio
66+
audio.play();
67+
//console.log(key);
68+
key.classList.add('playing');
69+
}
70+
71+
function removeTransition(e){
72+
if(e.propertyName !== 'transform') return;
73+
this.classList.remove('playing');
74+
}
75+
76+
const keys = document.querySelectorAll('.key');
77+
78+
keys.forEach(key => key.addEventListener('transitionend',removeTransition));
79+
window.addEventListener('keydown', playSound);
6580
</script>
6681

6782

0 commit comments

Comments
 (0)