Skip to content

Instantly share code, notes, and snippets.

@monbang
Last active March 25, 2020 18:46
Show Gist options
  • Save monbang/6680b2b00ffba0400e82cce0fdcabf74 to your computer and use it in GitHub Desktop.
Save monbang/6680b2b00ffba0400e82cce0fdcabf74 to your computer and use it in GitHub Desktop.
js-addEventListener
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