Skip to content

Commit 90cf9d7

Browse files
committed
Add my solution for JS30 wesbos#1
1 parent 7c04fd5 commit 90cf9d7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

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

6161
<script>
62+
63+
64+
function playSound(e) {
65+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
66+
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
67+
if (!audio) return;
68+
audio.currentTime = 0; // rewind to start.
69+
audio.play();
70+
if (!key) return;
71+
key.classList.add('playing');
72+
}
73+
74+
function removeTransition(e) {
75+
// Skip is not a transform.
76+
if (e.propertyName !== 'transform') return;
77+
this.classList.remove('playing');
78+
}
79+
80+
const keys = document.querySelectorAll('.key');
81+
// This is based on the CSS transitionend property. This is to make sure that the highlight will be removed after transition to yellow border.
82+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
83+
84+
window.addEventListener('keydown', playSound);
85+
86+
// window.addEventListener('keyup', function(e) {
87+
// const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
88+
// if (!key) return;
89+
// key.classList.remove('playing');
90+
// })
91+
6292

6393
</script>
6494

0 commit comments

Comments
 (0)