forked from VikramSingh138/Incredia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflip-book.js
121 lines (108 loc) · 3.35 KB
/
flip-book.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
114
115
116
117
118
119
120
121
// References to DOM Elements
const prevBtn = document.querySelector("#prev-btn");
const nextBtn = document.querySelector("#next-btn");
const book = document.querySelector("#book");
const paper1 = document.querySelector("#p1");
const paper2 = document.querySelector("#p2");
const paper3 = document.querySelector("#p3");
const paper4 = document.querySelector("#p4");
const paper5 = document.querySelector("#p5");
// Event Listener
prevBtn.addEventListener("click", goPrevPage);
nextBtn.addEventListener("click", goNextPage);
prevBtn.style.display = "none";
// Business Logic
let currentLocation = 1;
let numOfPapers = 5;
let maxLocation = numOfPapers + 1;
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
if (event.keyCode == '37') {
goPrevPage();
}
else if (event.keyCode == '39') {
goNextPage();
}
}
function openBook() {
book.style.transform = "translateX(35%)";
prevBtn.style.display = "block";
prevBtn.style.transform = "translateX(-290px)";
nextBtn.style.transform = "translateX(160px)";
}
function closeBook(isAtBeginning) {
if (isAtBeginning) {
book.style.transform = "translateX(0%)";
prevBtn.style.display = "none";
nextBtn.style.display = "block";
} else {
book.style.transform = "translateX(100%)";
nextBtn.style.display = "none";
}
prevBtn.style.transform = "translateX(0px)";
nextBtn.style.transform = "translateX(0px)";
}
function goNextPage() {
if (currentLocation < maxLocation) {
switch (currentLocation) {
case 1:
openBook();
paper1.classList.add("flipped");
paper1.style.zIndex = 1;
break;
case 2:
paper2.classList.add("flipped");
paper2.style.zIndex = 2;
break;
case 3:
paper3.classList.add("flipped");
paper3.style.zIndex = 3;
break;
case 4:
paper4.classList.add("flipped");
paper4.style.zIndex = 4;
break;
case 5:
paper5.classList.add("flipped");
paper5.style.zIndex = 5;
closeBook(false);
break;
default:
throw new Error("unkown state");
}
currentLocation++;
}
}
function goPrevPage() {
if (currentLocation > 1) {
switch (currentLocation) {
case 2:
closeBook(true);
paper1.classList.remove("flipped");
paper1.style.zIndex = 5;
break;
case 3:
paper2.classList.remove("flipped");
paper2.style.zIndex = 4;
break;
case 4:
paper3.classList.remove("flipped");
paper3.style.zIndex = 3;
break;
case 5:
paper4.classList.remove("flipped");
paper4.style.zIndex = 2;
break;
case 6:
openBook();
paper5.classList.remove("flipped");
paper5.style.zIndex = 1;
nextBtn.style.display = "block";
break;
default:
throw new Error("unkown state");
}
currentLocation--;
}
}