-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
38 lines (32 loc) · 1018 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
document.addEventListener('keydown',playKeySound);
const divsDataKeys = document.querySelectorAll(".key");
// remove classList firts
divsDataKeys.forEach(element => element.addEventListener('transitionend',removeTransition));
/**
* Play Key Sound: just play sound by key press.
*
* @param {Event} e event to raised.
* @return {void}
*/
function playKeySound(e){
const keyCode = e.keyCode;
const divDataKey = document.querySelector(`[data-key= '${keyCode}']`);
if(!divDataKey) return;
// add class
divDataKey.classList.add('playing');
// play sound
const audio = document.querySelector(`audio[data-key="${ keyCode }"]`);
// Play the audio in the same way as you currently are
audio.currentTime = 0;
audio.play();
}
/**
* Remove Transition: just remove class playing by key press.
*
* @param {Event} e event to raised.
* @return {void}
*/
function removeTransition(e){
if (e.propertyName !== 'transform') return;
this.classList.remove('playing');
}