-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
113 lines (96 loc) · 3.76 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
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
107
108
109
110
111
112
113
//player details
const players = [
{
name: document.querySelector("#playerOneName"),
score: document.querySelector("#playerOneScore"),
scoreBtn: document.querySelector("#playerOneScoreBtn"),
},
{
name: document.querySelector("#playerTwoName"),
score: document.querySelector("#playerTwoScore"),
scoreBtn: document.querySelector("#playerTwoScoreBtn"),
},
];
const totalPlayers = players.length; //total number of players
const resetBtn = document.querySelector("#resetBtn"); //reset button
const maxScore = document.querySelector("#maxScore"); //maximum score
const confetti = document.querySelector("#confetti"); //confetti overlay
//score updating function
function updateScore(players, playingOrder) {
let playerIndex = playingOrder - 1; //playing index is one less than playing order.
let maxScoreValue = maxScore.value;
players[playerIndex].score.textContent =
parseInt(players[playerIndex].score.textContent) + 1;
if (players[playerIndex].score.textContent == maxScoreValue) {
for (let i = 0; i < totalPlayers; i++) {
players[i].scoreBtn.setAttribute("disabled", "true");
if (i === playerIndex) {
players[i].score.classList.add("has-text-success");
} else {
players[i].score.classList.add("has-text-danger");
}
}
confetti.classList.add("display-block");
}
}
//reset score function
function reset(players) {
for (let i = 0; i < totalPlayers; i++) {
players[i].score.textContent = 0;
players[i].score.classList.remove("has-text-success");
players[i].score.classList.remove("has-text-danger");
players[i].scoreBtn.removeAttribute("disabled");
}
confetti.classList.remove("display-block");
}
for (let i = 0; i < totalPlayers; i++) {
players[i].scoreBtn.addEventListener("click", () => {
//clicking increment score button increases score
updateScore(players, i + 1);
});
}
resetBtn.addEventListener("click", () => {
// clicking reset score button resets score.
maxScoreValue = maxScore.value;
reset(players);
});
// Modal Functionality
const modal = document.querySelector(".modal");
const modalSaveBtn = document.querySelector("#modalSaveBtn");
modal.classList.add("display-block"); //By default Modal is visible on page load
modalSaveBtn.addEventListener("click", () => {
//naming button functionality
for (let i = 0; i < totalPlayers; i++) {
players[i].scoreBtn.textContent = `+1 [${
players[i].name.value || `Player ${i + 1}`
}]`;
}
//After 3s modal hides after save button click.
setTimeout(() => {
modal.classList.remove("display-block");
}, 3000);
});
// tt animation
//DOM elements
const ttAnimCont = document.querySelector(".tt-animation-container");
const ttLottie = document.querySelector("#tt-animation");
const tl = gsap.timeline(); //FadeOut Timeline
tl.pause(); //By default timline is paused
tl.to(".fadeOutAnimation", { y: 40, opacity: 0, stagger: 0.1 });
modalSaveBtn.addEventListener("click", () => {
tl.play(); //On clicking save button timeline plays.
setTimeout(() => {
//Fade elments and form are hidden with display none, so they woun`t take space.
document.querySelector(".Players-name").style.display = "none";
document.querySelectorAll(".fadeOutAnimation").forEach((element) => {
element.style.display = "none";
});
}, 1000);
setTimeout(() => {
//First tt bat animation is displayed with display block but with 0 opacity,
// later opacity increases gradually
ttAnimCont.classList.add("display-block");
gsap.from(ttAnimCont, { opacity: 0 });
ttLottie.play();
}, 1000);
});