Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d5ea597

Browse files
committed
keydown event correctl logs element if corresponding wav file exists.
1 parent a0451b1 commit d5ea597

File tree

3 files changed

+9
-100
lines changed

3 files changed

+9
-100
lines changed

01 - JavaScript Drum Kit/index-FINISHED.html

Lines changed: 0 additions & 83 deletions
This file was deleted.

01 - JavaScript Drum Kit/index-START.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
<script>
6161

62+
6263
</script>
6364

6465

01 - JavaScript Drum Kit/index.html

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,14 @@
5959

6060
<script>
6161

62-
63-
function playSound(e) {
64-
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
65-
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
66-
if (!audio) return; // stop the function from running all together
67-
audio.currentTime = 0; // rewind to the start
68-
audio.play();
69-
key.classList.add('playing');
70-
}
71-
function removeTransition(e) {
72-
if (e.propertyName !== 'transform') return; // skip it if it's not a transform
73-
this.classList.remove('playing');
74-
}
75-
76-
const keys = document.querySelectorAll('.key');
77-
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
78-
window.addEventListener('keydown', playSound);
62+
window.addEventListener("keydown", function (e) {
63+
const audio = document.querySelector(`audio[data-key='${e.keyCode}']`);
64+
const key = document.querySelector(`.key[data-key='${e.keyCode}']`);
65+
if(!audio) return;
66+
audio.currentTime = 0; //rewind to start
67+
audio.play()
68+
key.classList.add('playing')
69+
});
7970

8071
</script>
8172

0 commit comments

Comments
 (0)