-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
87 lines (79 loc) · 2.71 KB
/
script.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function playSound(e) {
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
if (!audio) return; // stops the function from running all together
audio.currentTime = 0; // allows the sound to start again or repeat when pressed
audio.play();
key.classList.add('playing'); // adds animation from my css
};
// Onclick functions
function clap() {
const clap = document.querySelector("audio[data-key='65']");
clap.currentTime = 0;
clap.play();
const key = document.querySelector(".key[data-key='65']");
key.classList.add('playing'); // adds the css animation while its playing
}
function hihat() {
const hihat = document.querySelector("audio[data-key='83']");
hihat.currentTime = 0;
hihat.play();
const key = document.querySelector(".key[data-key='83']");
key.classList.add('playing');
}
function kick() {
const kick = document.querySelector("audio[data-key='68']");
kick.currentTime = 0;
kick.play();
const key = document.querySelector(".key[data-key='68']");
key.classList.add('playing');
}
function openhat() {
const openhat = document.querySelector("audio[data-key='70']");
openhat.currentTime = 0;
openhat.play();
const key = document.querySelector(".key[data-key='70']");
key.classList.add('playing');
}
function boom() {
const boom = document.querySelector("audio[data-key='71']");
boom.currentTime = 0;
boom.play();
const key = document.querySelector(".key[data-key='71']");
key.classList.add('playing');
}
function ride() {
const ride = document.querySelector("audio[data-key='72']");
ride.currentTime = 0;
ride.play();
const key = document.querySelector(".key[data-key='72']");
key.classList.add('playing');
}
function snare() {
const snare = document.querySelector("audio[data-key='74']");
snare.currentTime = 0;
snare.play();
const key = document.querySelector(".key[data-key='74']");
key.classList.add('playing');
}
function tom() {
const tom = document.querySelector("audio[data-key='75']");
tom.currentTime = 0;
tom.play();
const key = document.querySelector(".key[data-key='75']");
key.classList.add('playing');
}
function tink() {
const tink = document.querySelector("audio[data-key='76']");
tink.currentTime = 0;
tink.play();
const key = document.querySelector(".key[data-key='76']");
key.classList.add('playing');
}
function removeTransition(e) {
if(e.propertyName !== 'transform') return; // skip it if it's not a transform
this.classList.remove('playing');
}
const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionend',removeTransition));
window.addEventListener('keydown', playSound);