-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginate.html
51 lines (43 loc) · 1.45 KB
/
paginate.html
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
<script>
let currentPage = 0;
const pagesCount = 15;
document.addEventListener('contextmenu', function(ev) {
ev.preventDefault();
if (currentPage > 1) {
currentPage--;
}
generatePageButtons(currentPage, pagesCount)
return false;
}, false);
document.addEventListener('click', function(ev) {
ev.preventDefault();
if (currentPage < pagesCount) {
currentPage++;
}
generatePageButtons(currentPage, pagesCount)
return false;
}, false);
function generatePageButtons(
currentPage,
pagesCount,
startCount = 3,
middleCount = 2,
endCount = 3,
strSplit = '...'
) {
let pageBtns = [
...Array.from(new Array(startCount), (x, i) => i + 1),
...Array.from(new Array(1 + middleCount * 2), (x, i) => i + currentPage - middleCount),
...Array.from(new Array(endCount), (x, i) => i + pagesCount - endCount + 1),
].reduce( (acc, val) => {
if (val < 1 || val > pagesCount || acc.indexOf(val) !== -1) return acc;
if (val - acc[acc.length - 1] > 1) {
//acc.push(strSplit);
acc.push((val - acc[acc.length - 1] == 2) ? val - 1 : strSplit);
}
acc.push(val);
return acc;
}, []);
console.log(pageBtns);
}
</script>