-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpianoCSS.js
More file actions
35 lines (27 loc) · 1.03 KB
/
pianoCSS.js
File metadata and controls
35 lines (27 loc) · 1.03 KB
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
document.addEventListener("DOMContentLoaded", function() {
function removeTransition(e) {
if (e.propertyName !== 'box-shadow') return;
e.target.classList.remove('key-pressed');
}
function keyPlaying(e) {
var keyCod, key, sounds;
if( e.keyCode === undefined ) {
keyCode = e.target.getAttribute("data-key");
key = document.querySelector(`li[data-key="${keyCode}"]`);
sounds = document.querySelector(`audio[data-key="${keyCode}"]`);
} else {
key = document.querySelector(`li[data-key="${e.keyCode}"]`);
sounds = document.querySelector(`audio[data-key="${e.keyCode}"]`);
}
if (!sounds) return;
sounds.currentTime = 0;
sounds.play();
key.classList.add('key-pressed');
}
var keys = Array.from(document.querySelectorAll('.key'));
keys.forEach(function(element) {
element.addEventListener("click", keyPlaying);
element.addEventListener('transitionend', removeTransition);
});
window.addEventListener('keydown', keyPlaying);
});