Skip to content

Commit 613e45e

Browse files
1 done. Practiced ES6 without any transpilers
1 parent e2038b7 commit 613e45e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@
5959

6060
<script>
6161

62+
function playSound(event) {
63+
/* // ES5 version
64+
const audioKeyCode = event.keyCode;
65+
const audio = document.querySelector("audio[data-key='"+audioKeyCode+"']");
66+
*/
67+
const audio = document.querySelector(`audio[data-key="${event.keyCode}"]`);
68+
const key = document.querySelector(`.key[data-key="${event.keyCode}"]`);
69+
if(!audio) return; // stop running function all together
70+
audio.currentTime = 0; // resets or rewinds to the start
71+
audio.play();
72+
key.classList.add("playing");
73+
};
74+
75+
function removeTransition(e) {
76+
if(e.propertyName !== "transform") return; //skip it if it's not a transform
77+
this.classList.remove("playing");
78+
}
79+
80+
const keys = document.querySelectorAll(".key");
81+
keys.forEach(key => key.addEventListener("transitionend", removeTransition));
82+
83+
84+
window.addEventListener("keydown", playSound);
85+
86+
87+
6288
</script>
6389

6490

0 commit comments

Comments
 (0)