Last active
March 25, 2020 18:46
-
-
Save monbang/6680b2b00ffba0400e82cce0fdcabf74 to your computer and use it in GitHub Desktop.
js-addEventListener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.addEventListener('keydown', (e) => { | |
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`); | |
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`); | |
if (!audio) return; | |
audio.currentTime = 0; | |
audio.play(); | |
key.classList.add('playing'); | |
key.classList.remove('playing'); | |
key.classList.toggle('playing'); | |
key.classList.contains('playing'); | |
}); | |
function removeTransition(e) { | |
if (e.propertyName != 'transform') return; | |
this.classList.remove('playing'); | |
} | |
const keys = document.querySelectorAll('.key'); | |
keys.forEach(key => key.addEventListener('transitionend', removeTransition)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment