-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (81 loc) · 2.81 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"use strict";
// Elements
const elevator = document.querySelector(".elevator");
const arrowButtons = document.querySelectorAll(".arrow-button");
const buttonOne = document.querySelector(".button-1"); // Down button
const buttonTwo = document.querySelector(".button-2"); // Up button
const buttonThree = document.querySelector(".button-3"); // Down button
const buttonFour = document.querySelector(".button-4"); // Up button
const soundEffect = document.querySelector("#sound-effect");
// Constants
const floorTwo = "-126%";
const floorOne = "-26%";
const floorZero = elevator.style.top;
const shortDuration = "5s";
const longDuration = "10s";
let isTransitionInProgress = false; // Track Elevator Transition
// Helper Functions
function playAudioEffect() {
soundEffect.currentTime = 0; // Reset Audio
soundEffect.volume = 1; // Set Volume
soundEffect.play();
}
function moveElevatorTo(floor, duration) {
if (isTransitionInProgress) return;
return new Promise((resolve) => {
isTransitionInProgress = true; // Set Transition Status
elevator.style.transition = `top ${duration} ease`;
elevator.style.top = floor;
elevator.addEventListener(
"transitionend",
() => {
isTransitionInProgress = false; // Reset Transition Status
playAudioEffect(); // Play Sound Effect
resolve();
},
{ once: true }
);
});
}
// Add Togglable Functionality
arrowButtons.forEach((button) => {
button.addEventListener("click", function () {
if (isTransitionInProgress) return; // Prevent Multiple Clicks
this.classList.toggle("active");
});
});
// Top Floor
// Down Button
buttonOne.addEventListener("click", async function () {
if (elevator.style.top !== floorTwo) return this.classList.remove("active");
this.classList.remove("active");
if (buttonThree.classList.contains("active")) {
await moveElevatorTo(floorOne, shortDuration);
return buttonThree.classList.remove("active");
}
await moveElevatorTo(floorZero, longDuration);
});
// Middle Floor
// Up Button
buttonTwo.addEventListener("click", async function () {
if (elevator.style.top !== floorOne) return;
this.classList.remove("active");
await moveElevatorTo(floorTwo, shortDuration);
});
// Down Button
buttonThree.addEventListener("click", async function () {
if (elevator.style.top !== floorOne) return;
this.classList.remove("active");
await moveElevatorTo(floorZero, shortDuration);
});
// Ground Floor
// Up Button
buttonFour.addEventListener("click", async function () {
if (elevator.style.top !== floorZero) return this.classList.remove("active");
this.classList.remove("active");
if (buttonTwo.classList.contains("active")) {
await moveElevatorTo(floorOne, shortDuration);
return buttonTwo.classList.remove("active");
}
await moveElevatorTo(floorTwo, longDuration);
});