-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (54 loc) · 1.6 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var numberOfbuttons = document.querySelectorAll(".piano").length;
for(var i=0; i<numberOfbuttons;i++){
document.querySelectorAll(".piano")[i].addEventListener("click",function(){
var buttonClick = this.innerHTML;
makesound(buttonClick);
buttonAnimation(buttonClick);
});
document.addEventListener("keypress", function(event){
makesound(event.key);
buttonAnimation(event.key);
})
}
function makesound(key){
switch (key) {
case "w":
var key1 = new Audio("sound/C3.mp3");
key1.play();
break;
case "a":
var key2 = new Audio("sound/D3.mp3");
key2.play();
break;
case "s":
var key3 = new Audio("sound/E3.mp3");
key3.play();
break;
case "d":
var key4 = new Audio("sound/Eb3.mp3");
key4.play();
break;
case "j":
var key5 = new Audio("sound/F3.mp3");
key5.play();
break;
case "k":
var key6 = new Audio("sound/G3.mp3");
key6.play();
break;
case "l":
var key7 = new Audio("sound/Gb3.mp3");
key7.play();
break;
default:
console.log();
break;
}
}
function buttonAnimation(button){
var currButton = document.querySelector("." + button);
currButton.classList.add("pressed");
setTimeout(function () {
currButton.classList.remove("pressed")
}, 100);
}