forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (25 loc) · 769 Bytes
/
script.js
File metadata and controls
31 lines (25 loc) · 769 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
function windowLoaded () {
const keys = document.querySelectorAll('.key')
console.log(keys)
keys.forEach(key =>
key.addEventListener('transitionend', removePlayingTransition))
window.addEventListener('keydown', keyDown)
}
function keyDown (e) {
const keyPressed = e.keyCode
const audio = document.querySelector(`audio[data-key="${keyPressed}"]`)
if (audio) {
// start keycap transition
var keycap = document.querySelector(`.key[data-key="${keyPressed}"]`)
keycap.classList.add('playing')
audio.currentTime = 0
audio.play()
}
}
function removePlayingTransition (e) {
console.log(e.target)
if (e.propertyName === 'transform') {
e.target.classList.remove('playing')
}
}
window.addEventListener('load', windowLoaded)