Skip to content

Commit fb135a7

Browse files
committed
stable git for day 1
1 parent 45e08db commit fb135a7

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

01 - JavaScript Drum Kit/index-START.html

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

6060
<script>
61+
window.addEventListener('keypress', function(e) {
62+
// var key = e.key
63+
const key = e.keyCode
64+
const keys = [65, 83, 68, 70, 71, 72, 74, 75, 76]
6165

66+
if(keys.indexOf(key) !== -1) {
67+
transitionKey(key)
68+
playAudio(key)
69+
}
70+
})
71+
72+
function playAudio(key) {
73+
const audio = document.querySelector(`audio[data-key="${ key }"]`)
74+
if(! audio) return ;
75+
audio.currentTime = 0
76+
audio.play()
77+
}
78+
79+
function transitionKey(key) {
80+
const keyDiv = document.querySelector(`div[data-key="${ key }"]`)
81+
keyDiv.classList.add('playing')
82+
}
83+
84+
const keys = document.querySelectorAll('.key')
85+
keys.forEach(elem => {
86+
elem.addEventListener('transitionend', function(e) {
87+
if (e.propertyName !== 'transform') return;
88+
this.classList.remove('playing')
89+
})
90+
})
6291
</script>
6392

6493

0 commit comments

Comments
 (0)