Skip to content

Commit aecacf7

Browse files
committed
drum kit complete
1 parent 63c0fb4 commit aecacf7

File tree

1 file changed

+20
-0
lines changed
  • 01 - JavaScript Drum Kit/js

1 file changed

+20
-0
lines changed

01 - JavaScript Drum Kit/js/app.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function playSound(e) {
2+
const audio = document.querySelector(`audio[data-key= "${e.keyCode}"]`);
3+
const key = document.querySelector(`.key[data-key= "${e.keyCode}"]`);
4+
console.log(audio);
5+
if (!audio) return; // stop the function from running
6+
audio.currentTime = 0; // rewind to the start
7+
audio.play();
8+
key.classList.add('playing');
9+
}
10+
11+
function removeTransition(e) {
12+
if(e.propertyName !== 'transform') return; // skip if it's not a transform
13+
14+
this.classList.remove('playing');
15+
}
16+
17+
const keys = document.querySelectorAll('.key');
18+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
19+
20+
window.addEventListener('keydown', playSound);

0 commit comments

Comments
 (0)