Skip to content

Commit 72379c3

Browse files
authored
Update
Finished tutorial, still need to go back to add comments for things that are new to me for clarification.
1 parent b2a8914 commit 72379c3

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

01 - JavaScript Drum Kit/index.html

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,41 @@
99

1010
<!-- Buttons -->
1111
<!-- Data attributes (data-key) wire up buttons with sounds -->
12+
<!-- http://keycode.info - nice site for getting keycodes -->
1213
<div class="keys">
13-
<div data-key="65" class="key">
14+
<div data-key="65" class="js-key">
1415
<kbd>A</kbd>
1516
<span class="sound">clap</span>
1617
</div>
17-
<div data-key="83" class="key">
18+
<div data-key="83" class="js-key">
1819
<kbd>S</kbd>
1920
<span class="sound">hihat</span>
2021
</div>
21-
<div data-key="68" class="key">
22+
<div data-key="68" class="js-key">
2223
<kbd>D</kbd>
2324
<span class="sound">kick</span>
2425
</div>
25-
<div data-key="70" class="key">
26+
<div data-key="70" class="js-key">
2627
<kbd>F</kbd>
2728
<span class="sound">openhat</span>
2829
</div>
29-
<div data-key="71" class="key">
30+
<div data-key="71" class="js-key">
3031
<kbd>G</kbd>
3132
<span class="sound">boom</span>
3233
</div>
33-
<div data-key="72" class="key">
34+
<div data-key="72" class="js-key">
3435
<kbd>H</kbd>
3536
<span class="sound">ride</span>
3637
</div>
37-
<div data-key="74" class="key">
38+
<div data-key="74" class="js-key">
3839
<kbd>J</kbd>
3940
<span class="sound">snare</span>
4041
</div>
41-
<div data-key="75" class="key">
42+
<div data-key="75" class="js-key">
4243
<kbd>K</kbd>
4344
<span class="sound">tom</span>
4445
</div>
45-
<div data-key="76" class="key">
46+
<div data-key="76" class="js-key">
4647
<kbd>L</kbd>
4748
<span class="sound">tink</span>
4849
</div>
@@ -60,9 +61,27 @@
6061
<audio data-key="76" src="sounds/tink.wav"></audio>
6162

6263
<script>
63-
window.addEventListener('keydown', function (e) {
64-
console.log(e);
65-
});
64+
65+
function playSound(e) {
66+
// console.log(e.keyCode);
67+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
68+
const key = document.querySelector(`.js-key[data-key="${e.keyCode}"]`);
69+
if (!audio) return; // if the key pressed isn't a match stop the function from running
70+
audio.currentTime = 0; // rewind audio clip to start
71+
audio.play();
72+
key.classList.add('is-playing');
73+
}
74+
75+
function removeTransition(e) {
76+
if(e.propertyName !== 'transform') return // skip if it's not a transform
77+
// console.log(e.propertyName);
78+
this.classList.remove('is-playing');
79+
}
80+
81+
const keys = document.querySelectorAll('.js-key');
82+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
83+
84+
window.addEventListener('keydown', playSound);
6685

6786
</script>
6887

0 commit comments

Comments
 (0)