Skip to content

Commit 5601689

Browse files
committed
Sounds and animation transition
1 parent 3b6b6e9 commit 5601689

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

01 - JavaScript Drum Kit/index-START.html

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

6060
<script>
61-
window.addEventListener('keydown', function (e) {
61+
function playSound (e) {
6262
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
63+
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
6364
if (!audio) return; // this will stop the function from running all together
64-
65+
audio.currentTime = 0; // rebobina al principio y me permite reproducir cada vez que pulso la tecla
6566
audio.play();
66-
});
67+
68+
key.classList.add('playing');
69+
}
70+
71+
window.addEventListener('keydown', playSound);
72+
73+
function removeTransition(e) {
74+
if (e.propertyName !== 'transform') return; // lo debe saltar si la propiedad no es transform
75+
this.classList.remove('playing');
76+
}
77+
78+
const keys = document.querySelectorAll('.key');
79+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
6780
</script>
6881

6982

0 commit comments

Comments
 (0)