moan.js 672 B

123456789101112131415161718192021222324
  1. window.addEventListener('keydown', function(e) {
  2. console.log(e);
  3. })
  4. function removeTransition(e) {
  5. if (e.propertyName !== 'transform') return;
  6. e.target.classList.remove('playing');
  7. }
  8. function playSound(e) {
  9. const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
  10. const key = document.querySelector(`div[data-key="${e.keyCode}"]`);
  11. if (!audio) return;
  12. key.classList.add('playing');
  13. audio.currentTime = 0;
  14. audio.play();
  15. }
  16. const keys = Array.from(document.querySelectorAll('.key'));
  17. keys.forEach(key => key.addEventListener('transitionend', removeTransition));
  18. window.addEventListener('keydown', playSound);