Visitar URL original
finished 01 · wesbos/JavaScript30@f56fcb2 · GitHub
Skip to content

Commit f56fcb2

Browse files
committed
finished 01
1 parent 38a754c commit f56fcb2

3 files changed

Lines changed: 17 additions & 156 deletions

File tree

‎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: 0 additions & 66 deletions
This file was deleted.

‎01 - JavaScript Drum Kit/index.html‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,35 @@
5959

6060
<script>
6161

62-
63-
function playSound(e) {
62+
function playsound(e) {
63+
// listen for keys, if a keyCode is bound to an audio attribute sore it in the audio variable
6464
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
65+
// get the key element
6566
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
67+
// stop the function if a a key was pressed that has no audio attribute
68+
if(!audio) return;
69+
// rewind audio clip to the start.
70+
audio.currentTime = 0;
6871
audio.play();
72+
// add the playing css to the element that was triggered
6973
key.classList.add('playing');
7074
}
75+
7176
function removeTransition(e) {
72-
if (e.propertyName !== 'transform') return; // skip it if it's not a transform
73-
this.classList.remove('playing');
77+
// skip it if it is not a transform
78+
if (e.propertyName !== 'transform') return;
79+
// since a key element called this function, we can refer to it as this
80+
this.classList.remove('playing')
7481
}
7582

83+
// add an event listening to all key elements for when have tranistioned after playing a drum loop
7684
const keys = document.querySelectorAll('.key');
7785
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
78-
window.addEventListener('keydown', playSound);
7986

87+
// list for the keydown event
88+
window.addEventListener('keydown', playsound);
8089
</script>
8190

91+
8292
</body>
8393
</html>

0 commit comments

Comments
 (0)