forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
34 lines (30 loc) · 894 Bytes
/
script.js
File metadata and controls
34 lines (30 loc) · 894 Bytes
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
'use strict';
function playsound(keycode) {
const audio = document.querySelector(`audio[data-key="${keycode}"]`);
if (!audio) {
return;
}
audio.currentTime = 0; //rewind
audio.play();
}
function animate(keycode) {
const key = document.querySelector(`.key[data-key="${keycode}"]`);
if (!key) {
return;
}
key.classList.add('playing');
}
function removeTransition(e) {
if (e.propertyName === 'transform') {
// note : this = e.target dans le contexte d'une fonction appelée pour un evenement
this.classList.remove('playing');
}
}
window.addEventListener('keydown', function (e) {
const keycode = e.keyCode;
console.log('keycode: ', keycode);
playsound(keycode);
animate(keycode);
const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
});