Skip to content

Commit 6c26663

Browse files
committed
Add solution; 01
1 parent c322162 commit 6c26663

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

01 - JavaScript Drum Kit/index-START.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,31 @@
5858
<audio data-key="76" src="sounds/tink.wav"></audio>
5959

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

6283
</script>
6384

6485

6586
</body>
6687
</html>
88+

01 - JavaScript Drum Kit/style.css

Lines changed: 3 additions & 3 deletions
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;
2727
width: 10rem;
2828
text-align: center;
2929
color: white;
@@ -32,8 +32,8 @@ body,html {
3232
}
3333

3434
.playing {
35-
transform: scale(1.1);
36-
border-color: #ffc600;
35+
transform: scale(1.2);
36+
border-color: #ccc600;
3737
box-shadow: 0 0 1rem #ffc600;
3838
}
3939

0 commit comments

Comments
 (0)