Skip to content

Commit 1e8c2e8

Browse files
author
mjdude
committed
added remove transition function and play sound function
1 parent f38f884 commit 1e8c2e8

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,35 @@
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}"]`); // . selects class. Can also use div [data-key="${e.keyCode}"]
64-
console.log(key);
63+
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`); // . selects class. Can also use div [data-key="${e.keyCode}"]
6564
if (!audio) {
6665
return; // stop function from running if key is not found
6766
}
6867
audio.currentTime = 0; // rewinds to the start of the file
6968
audio.play();
70-
})
69+
// same as using jQuery key.addClass('.playing'); add's playing class from css
70+
// can also use .remove and .toggle to change classes
71+
key.classList.add('playing');
7172

73+
}
74+
function removeTransition(e){
75+
if (e.propertyName !== 'transform' ) {
76+
return; // skip any transition that isnt transform
77+
}
78+
79+
// this is equal to what was called against it, in this case this = key
80+
this.classList.remove('playing');
81+
82+
};
83+
84+
const keys = document.querySelectorAll(`.key`);
85+
keys.forEach((key) => {
86+
key.addEventListener('transitionend', removeTransition)
87+
});
88+
89+
window.addEventListener('keydown', playSound);
7290
</script>
7391

7492

0 commit comments

Comments
 (0)