Skip to content

Commit cae2897

Browse files
committed
Add and remove playing class when key pressed with transition
1 parent 56771ad commit cae2897

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,24 @@
6060
<script>
6161
window.addEventListener('keydown', function(e) {
6262
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
63-
console.log(audio)
63+
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
6464
if(!audio) return; //stop function all together
6565
// note if call .play on audio element already playing, it won't play again. so if press "F" in successsion it will only play every 5 sec
6666
audio.currentTime = 0; //rewind to the start, so that it can be played many times quickly
6767
audio.play();
68-
68+
// CSS: transition:all .07s;
69+
//.playing adds transform:scale(1.1);
70+
key.classList.add('playing');
6971
});
72+
73+
function removeTransition(e) {
74+
if (e.propertyName !== 'transform') return; // skip it if its not a transform
75+
// here this = key div
76+
this.classList.remove('playing');
77+
}
78+
79+
const keys = document.querySelectorAll('.key');
80+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
7081
</script>
7182

7283

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;
26+
transition:all .1s;
2727
width:100px;
2828
text-align: center;
2929
color:white;

0 commit comments

Comments
 (0)