Skip to content

Commit 0e49f5a

Browse files
committed
Day 01 - JavaSCript Drum Kit
1 parent 508e476 commit 0e49f5a

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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>
8-
<body>
99

10+
<body>
1011

1112
<div class="keys">
1213
<div data-key="65" class="key">
@@ -57,10 +58,7 @@
5758
<audio data-key="75" src="sounds/tom.wav"></audio>
5859
<audio data-key="76" src="sounds/tink.wav"></audio>
5960

60-
<script>
61-
62-
</script>
63-
64-
61+
<script src="index.js"></script>
6562
</body>
66-
</html>
63+
64+
</html>

01 - JavaScript Drum Kit/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[...document.querySelectorAll('.key')]
2+
.forEach(key => {
3+
key.addEventListener('transitionend', (e) => {
4+
// This event is called for each property that ended a transition.
5+
6+
key.classList.remove('playing');
7+
});
8+
});
9+
10+
window.addEventListener('keyup', ({ keyCode }) => {
11+
const keyElement = document.querySelector(`.key[data-key="${keyCode}"]`);
12+
const audioElement = document.querySelector(`audio[data-key="${keyCode}"]`);
13+
14+
if (keyElement && audioElement) {
15+
audioElement.currentTime = 0; // rewind to start
16+
audioElement.play();
17+
18+
keyElement.classList.add('playing');
19+
}
20+
});

01 - JavaScript Drum Kit/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ body,html {
2323
margin: 1rem;
2424
font-size: 1.5rem;
2525
padding: 1rem .5rem;
26-
transition: all .07s ease;
26+
transition: all .15s ease-in-out;
2727
width: 10rem;
2828
text-align: center;
2929
color: white;

0 commit comments

Comments
 (0)