From f3fd9da738a8b0f9295dc40d3dd1de23a5dc3531 Mon Sep 17 00:00:00 2001 From: Przemek Date: Thu, 8 Dec 2016 19:43:23 +0100 Subject: [PATCH] day 01 --- 01 - JavaScript Drum Kit/index-START.html | 24 ++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/01 - JavaScript Drum Kit/index-START.html b/01 - JavaScript Drum Kit/index-START.html index 4070d32767..6cd6596167 100644 --- a/01 - JavaScript Drum Kit/index-START.html +++ b/01 - JavaScript Drum Kit/index-START.html @@ -59,8 +59,30 @@ + document.addEventListener('keydown', function(ev) { + const keyCode = ev.keyCode; + + const keyRef = document.querySelector(`.key[data-key="${keyCode}"`); + const audioRef = document.querySelector(`audio[data-key="${keyCode}"`); + + if(keyRef) { + keyRef.classList.add('playing'); + + keyRef.addEventListener('transitionend', function(ev) { + if (ev.propertyName !== 'transform') return; + ev.target.classList.remove('playing'); + }); + } + if(audioRef) { + const audioPath = audioRef.attributes['src'].value; + const audioObj = new Audio(audioPath); + audioObj.play(); + } + + }); + +