-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginations.js
109 lines (92 loc) · 3.2 KB
/
paginations.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
const articlesSelector = ".article-cols > div";
let articles = $(articlesSelector);
const prevBtn = $(".btn-prev");
const nextBtn = $(".btn-next");
const clearBtn = $(".btn-filter-clear-paragraph");
const counter = $(".pagination-counter");
const itemsPerPage = 6;
let currentPage = 1;
const btnsFilter = $(".btn-filter-blog");
updateCounter();
articles.hide();
articles.slice(0, itemsPerPage).fadeIn();
const numberOfPages = Math.ceil(articles.length / itemsPerPage);
prevBtn.on("click", () => {
if (currentPage > 1) {
currentPage--;
articles.hide();
articles.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage).fadeIn();
updateCounter();
scrollToTopArticles();
}
});
nextBtn.on("click", () => {
if (currentPage < Math.ceil(articles.length / itemsPerPage)) {
currentPage++;
articles.hide();
articles.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage).fadeIn();
updateCounter()
scrollToTopArticles();
}
});
btnsFilter.on("click", (e) => {
// remove all style css from buttons
btnsFilter.removeAttr("style");
// get value of attribute data-filter
const filterName = e.target.getAttribute("data-filter");
const filterColor = e.target.getAttribute("data-color");
if (!filterName || !filterName.length) return;
e.target.parentElement.style.backgroundColor = filterColor;
e.target.parentElement.style.color = "#fff";
articles.hide();
articles = $(articlesSelector).filter(`[data-filter="${filterName}"]`);
// show with fade effect
articles.slice(0, itemsPerPage).fadeIn();
currentPage = 1;
updateCounter();
});
clearBtn.on("click", () => {
articles.hide();
articles = $(articlesSelector);
// uncheck all radio buttons
$("input[type=radio]").prop("checked", false);
btnsFilter.removeAttr("style");
articles.slice(0, itemsPerPage).fadeIn();
updateCounter();
});
function updateCounter() {
const maxPages = Math.ceil(articles.length / itemsPerPage);
// if current page is 1 add btn-disabled class to prevBtn
if (currentPage === 1) {
prevBtn.addClass("btn-disabled");
} else {
prevBtn.removeClass("btn-disabled");
}
// if current page is last add btn-disabled class to nextBtn
if (currentPage === maxPages) {
nextBtn.addClass("btn-disabled");
} else {
nextBtn.removeClass("btn-disabled");
}
// clean pagination
counter.empty();
for (let i = 0; i < maxPages; i++) {
// add button to pagination
if ($(`.btn-page-${i + 1}`).length === 0) {
const btn = $(`<button class="btn-page btn-page-${i + 1} ${currentPage === i + 1 ? "active" : ""}">${i + 1}</button>`);
counter.append(btn);
btn.on("click", () => {
currentPage = i + 1;
articles.hide();
articles.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage).fadeIn();
updateCounter();
scrollToTopArticles();
});
}
}
}
function scrollToTopArticles() {
// $("html, body").animate({
// scrollTop: $(".article-cols").offset().top
// }, 1000);
}