Visitar URL original
Drum kit · wesbos/JavaScript30@57fa6a0 · GitHub
Skip to content

Commit 57fa6a0

Browse files
committed
Drum kit
1 parent 38a754c commit 57fa6a0

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

‎01 - JavaScript Drum Kit/index-START.html‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,46 @@
5959

6060
<script>
6161

62-
</script>
62+
let keys = document.getElementsByClassName('key');
63+
let keyCode = '';
64+
let audioElement;
65+
66+
window.onkeydown = function(e){
67+
68+
// get keycode
69+
keyCode = e.keyCode;
70+
71+
// get key with key code
72+
let key = document.querySelector('[data-key="'+keyCode+'"].key');
73+
74+
// don't do anything if the key doesn't exist
75+
if(!key) return;
76+
77+
// get audio with key code
78+
audioElement = document.querySelector('audio[data-key="'+keyCode+'"]')
79+
80+
// add class to key
81+
key.className = key.className + ' playing';
6382

83+
// play audio
84+
audioElement.currentTime = 0;
85+
audioElement.play();
86+
87+
// remove class from key
88+
setTimeout(function(){
89+
key.classList.remove('playing');
90+
}, 100);
91+
92+
}
93+
94+
function playSound( audioEl ) {
95+
console.log(audioEl)
96+
audioEl.play();
97+
}
98+
99+
100+
101+
</script>
64102

65103
</body>
66104
</html>

0 commit comments

Comments
 (0)