-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (43 loc) · 1.47 KB
/
app.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const blackKeys = document.querySelectorAll('.key.black')
const blackOrder = ['Db', 'Eb', 'Gb', 'Ab', 'Bb']
const blackKeyboard = ['2', '3', '5', '6', '7']
const whiteKeys = document.querySelectorAll('.key.white')
const whiteOrder = ['C', 'D', 'E', 'F', 'G', 'A', 'B']
const whiteKeyboard = ['q', 'w', 'e', 'r', 't', 'y', 'u']
let audio
blackKeys.forEach((key, index) => {
key.addEventListener('click', () => {
audio = new Audio(`./notes/${blackOrder[index]}.mp3`)
audio.currentTime = 0
audio.play()
})
})
whiteKeys.forEach((key, index) => {
key.addEventListener('click', () => {
audio = new Audio(`./notes/${whiteOrder[index]}.mp3`)
audio.currentTime = 0
audio.play()
})
})
document.addEventListener('keyup', e => {
whiteKeyboard.forEach((key, index) => {
if(key == e.key) {
audio = new Audio(`./notes/${whiteOrder[index]}.mp3`)
audio.currentTime = 0
audio.play()
whiteKeys[index].style.opacity = '0.7'
setTimeout(() => whiteKeys[index].style.opacity = '1', 500)
}
})
})
document.addEventListener('keyup', e => {
blackKeyboard.forEach((key, index) => {
if(key == e.key) {
audio = new Audio(`./notes/${blackOrder[index]}.mp3`)
audio.currentTime = 0
audio.play()
blackKeys[index].style.opacity = '0.7'
setTimeout(() => blackKeys[index].style.opacity = '1', 500)
}
})
})