-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
65 lines (57 loc) · 1.84 KB
/
scripts.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
const allVideos = document.querySelectorAll("video");
const allAudios = document.querySelectorAll("audio");
function stopAllAudio() {
allAudios.forEach(function (audio) {
audio.pause();
});
}
const playVideo = (e) => {
let keyCode;
if (e.type === "keydown") {
keyCode = e.keyCode;
} else {
keyCode =
e.target.getAttribute("data-key") ||
e.target.parentNode.getAttribute("data-key");
}
const key = document.querySelector(`div[data-key="${keyCode}"]`);
const videos = document.querySelector(`video[data-key="${keyCode}"]`);
const allVideos = document.querySelector("video");
Array.from(document.querySelectorAll("video")).forEach(function (allVideos) {
allVideos.classList.remove("playing");
});
allVideos.classList.remove("playing");
videos.classList.add("playing");
videos.currentTime = 0;
};
const playSound = (e) => {
let keyCode;
if (e.type === "keydown") {
keyCode = e.keyCode;
} else {
keyCode =
e.target.getAttribute("data-key") ||
e.target.parentNode.getAttribute("data-key");
}
const audio = document.querySelector(`audio[data-key="${keyCode}"]`);
const key = document.querySelector(`div[data-key="${keyCode}"]`);
if (!audio) return;
stopAllAudio();
key.classList.add("playing");
audio.currentTime = 0;
audio.play();
};
const removeTransition = (e) => {
if (e.propertyName !== "transform") return;
e.target.classList.remove("playing");
};
const keys = Array.from(document.querySelectorAll(".key"));
keys.forEach((key) => {
key.addEventListener("transitionend", removeTransition);
});
window.addEventListener("keydown", playSound);
window.addEventListener("touchstart", playSound);
window.addEventListener("click", playSound);
window.addEventListener("keydown", playVideo);
window.addEventListener("touchstart", playVideo);
window.addEventListener("click", playVideo);