forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
24 lines (21 loc) · 677 Bytes
/
scripts.js
File metadata and controls
24 lines (21 loc) · 677 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
/* global Audio */
function removeTransition (e) {
if (e.propertyName !== 'transform') return
e.target.classList.remove('playing')
}
function handlekey (e) {
playSoundByKey(e.keyCode)
}
function playSoundByKey (keyCode) {
const keyElement = document.querySelector(`div[data-key="${keyCode}"]`)
if (!keyElement) {
return
}
keyElement.classList.add('playing')
const audioPath = keyElement.getAttribute('data-audioPath')
const audio = new Audio(audioPath)
audio.play()
}
const keys = Array.from(document.querySelectorAll('.key'))
keys.forEach(key => key.addEventListener('transitionend', removeTransition))
window.addEventListener('keydown', handlekey)