Visitar URL original
Lesson 01 · wesbos/JavaScript30@04834d2 · GitHub
Skip to content

Commit 04834d2

Browse files
Cedric MagninCedric Magnin
authored andcommitted
Lesson 01
1 parent 61b13da commit 04834d2

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

‎01 - JavaScript Drum Kit/index-START.html‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010

1111
<div class="keys">
12-
<div data-key="65" class="key">
13-
<kbd>A</kbd>
12+
<div data-key="81" class="key">
13+
<kbd>Q</kbd>
1414
<span class="sound">clap</span>
1515
</div>
1616
<div data-key="83" class="key">
@@ -47,7 +47,7 @@
4747
</div>
4848
</div>
4949

50-
<audio data-key="65" src="sounds/clap.wav"></audio>
50+
<audio data-key="81" src="sounds/clap.wav"></audio>
5151
<audio data-key="83" src="sounds/hihat.wav"></audio>
5252
<audio data-key="68" src="sounds/kick.wav"></audio>
5353
<audio data-key="70" src="sounds/openhat.wav"></audio>
@@ -59,6 +59,24 @@
5959

6060
<script>
6161

62+
function playSound(e) {
63+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
64+
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
65+
if(!audio) return; // stop
66+
audio.currentTime = 0;
67+
audio.play();
68+
key.classList.add('playing');
69+
}
70+
71+
function removeTransition(e) {
72+
if(e.propertyName !== 'transform') return;
73+
this.classList.remove('playing');
74+
}
75+
76+
const keys = document.querySelectorAll('.key');
77+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
78+
window.addEventListener('keydown', playSound);
79+
6280
</script>
6381

6482

0 commit comments

Comments
 (0)