-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
64 lines (56 loc) · 1.76 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
const ulTag = document.querySelector("ul");
const totalPages = 20;
function element(totalPages, page) {
let liTag = "";
let activeLi;
let beforePages = page - 1;
let afterPages = page + 1;
if (page > 1) {
liTag += `<li class="btn prev" onclick = "element(totalPages, ${
page - 1
})"><span><i class="fas fa-angle-left"></i>Prev</span></li>`;
}
if (page > 2) {
liTag += `<li class="numb" onclick="element(totalPages, 1)"><span>1</span></li>`;
if (page > 3) {
liTag += `<li class="dots"><span>...</span></li>`;
}
}
if (page == 1) {
afterPages = afterPages + 2;
} else if (page == 2) {
afterPages = afterPages + 1;
}
if (page == totalPages) {
beforePages = beforePages - 2;
} else if (page == totalPages - 1) {
beforePages = beforePages - 1;
}
for (let pageLength = beforePages; pageLength <= afterPages; pageLength++) {
if (pageLength > totalPages) {
continue;
}
if (pageLength == 0) {
pageLength = pageLength + 1;
}
if (page === pageLength) {
activeLi = "active";
} else {
activeLi = "";
}
liTag += `<li class="numb ${activeLi}" onclick="element(totalPages, ${pageLength})"><span>${pageLength}</span></li>`;
}
if (page < totalPages - 1) {
if (page < totalPages-2) {
liTag += `<li class="dots"><span>...</span></li>`;
}
liTag += `<li class="numb" onclick="element(totalPages, ${totalPages})"><span>${totalPages}</span></li>`;
}
if (page < totalPages) {
liTag += `<li class="btn next" onclick = "element(totalPages, ${
page + 1
})"><span>Next<i class="fas fa-angle-right"></i></span></li>`;
}
ulTag.innerHTML = liTag;
}
element(totalPages, 5);