-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook_page.js
92 lines (80 loc) · 2.35 KB
/
book_page.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
// References to DOM Elements
const book = document.querySelector("#book");
const paper1 = document.querySelector("#p1");
const paper2 = document.querySelector("#p2");
const paper3 = document.querySelector("#p3");
//Asignación eventos a cada pagina de mi libro
const pagesFront = document.querySelectorAll('.front');
pagesFront.forEach(page => {
page.addEventListener('click', goNextPage);
});
const pagesBack = document.querySelectorAll('.back');
pagesBack.forEach(pagBack => {
pagBack.addEventListener('click', goPrevPage);
});
// Lógica
let currentLocation = 1;
let numOfPapers = 3;
let maxLocation = numOfPapers + 1;
function openBook() {
book.style.transform = "translateX(50%)";
}
function closeBook(isAtBeginning) {
if (isAtBeginning) {
book.style.transform = "translateX(0%)";
} else {
book.style.transform = "translateX(100%)";
}
}
function goNextPage(event) {
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;
closeBook(false);
break;
default:
throw new Error("unkown state");
}
currentLocation++;
}
}
const links = document.querySelectorAll('.front a');
links.forEach(link => {
link.addEventListener('click', event => {
event.stopPropagation();
});
});
function goPrevPage() {
if (currentLocation > 1) {
switch (currentLocation) {
case 2:
closeBook(true);
paper1.classList.remove("flipped");
paper1.style.zIndex = 3;
break;
case 3:
paper2.classList.remove("flipped");
paper2.style.zIndex = 2;
break;
case 4:
openBook();
paper3.classList.remove("flipped");
paper3.style.zIndex = 1;
break;
default:
throw new Error("unkown state");
}
currentLocation--;
}
}