Skip to content

Commit b3c7e7c

Browse files
committed
completed day 1 challenge
1 parent b8bc9cb commit b3c7e7c

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>JS Drum Kit</title>
67
<link rel="stylesheet" href="style.css">
78
</head>
9+
810
<body>
911

1012

@@ -57,10 +59,35 @@
5759
<audio data-key="75" src="sounds/tom.wav"></audio>
5860
<audio data-key="76" src="sounds/tink.wav"></audio>
5961

60-
<script>
62+
<script>
63+
const keys = document.querySelectorAll('.key');
64+
window.addEventListener('keydown', playSound);
65+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
66+
67+
/* ===== functions below ====== */
68+
function playSound(e) {
69+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
70+
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
71+
72+
if (!audio) return;
6173

62-
</script>
74+
audio.currentTime = 0;
75+
audio.play();
76+
key.classList.add('playing');
77+
78+
// console.log('key:', key);
79+
// console.log('audio:', audio);
80+
81+
};
82+
83+
function removeTransition(e) {
84+
if (e.propertyName !== 'transform') return;
85+
this.classList.remove('playing');
86+
// console.log('this : ', this);
87+
};
88+
</script>
6389

6490

6591
</body>
66-
</html>
92+
93+
</html>

0 commit comments

Comments
 (0)