Skip to content

Commit 64314d0

Browse files
committed
Complete 01. Extract functions for play sound and post play processing.
1 parent 38a754c commit 64314d0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

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

6060
<script>
61+
var highlightAndPlay = function(e) {
62+
var pressed = document.querySelector(`.key[data-key="${e.keyCode}"]`);
63+
if(!pressed) return;
64+
pressed.classList.add("playing");
65+
66+
var sound = document.querySelector(`audio[data-key="${e.keyCode}"]`);
67+
sound.currentTime = 0;
68+
sound.play();
69+
}
70+
document.addEventListener("keyup", highlightAndPlay);
71+
72+
var backToNormalAfterPlayed = function(key) {
73+
key.addEventListener("transitionend", function(e) {
74+
if(e.propertyName !== "transform") return;
75+
key.classList.remove("playing");
76+
});
77+
}
78+
document.querySelectorAll(".key").forEach(backToNormalAfterPlayed);
6179

6280
</script>
6381

0 commit comments

Comments
 (0)