Dia 38/2019 - Paginação Simples

Desafio #92daysofcode que fiz no final de 2019. Nesse artigo temos o dia 38, mostrando o uso de uma classe e seus métodos.

Pré-visualização

Código

Começamos com a estrutura do HTML.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Day 38</title>
<style>
*, *:before, *:after { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing:border-box; }
html { -webkit-box-sizing:border-box; -moz-box-sizing:border-box; box-sizing:border-box; }
* { margin: 0; padding: 0; list-style: none; outline: none; }
body { background: #000; color: #fff; font-family: Arial, Helvetica, sans-serif; }
.container { margin: 20px auto; max-width: 992px; text-align: center; width: 100%; }
</style>
<link rel="stylesheet" type="text/css" href="./day-38.css">
</head>
<body>
<div class="container">
<!-- pagination -->
</div>
<script src="./day-38.js"></script>
</body>
</html>
1
2
3
4
5
6
<div data-js="main"></div>
<div class="pagination" data-js="pagination">
<button class="pagination-button prev">&#9668;</button>
<ul class="pagination-list"></ul>
<button class="pagination-button next">&#9658;</button>
</div>
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
.pagination { padding: 15px 0; }

.pagination,
.pagination-list { align-items: center; display: flex; justify-content: center; flex-flow: row wrap; }
.pagination-item { background: #8b1ba1; text-align: center; position: relative; }

.pagination-button {
background: #b445e7;
border: 3px solid transparent;
border-radius: 50%;
color: #fff;
cursor: pointer;
display: block;
height: 35px;
transition: 0.1s;
width: 35px;
}

span {
background: #8b1ba1;
border: 3px solid transparent;
display: block;
height: 35px;
padding: 10px 5px 5px;
width: 35px;
}

.pagination-button:hover,
.pagination-button.active { border: 3px solid #fdff71; color: #fdff71; position: relative; transform: scale(1.3); z-index: 2; }

.pagination-button:hover { z-index: 3; }

.prev:hover,
.next:hover { border: 3px solid transparent; transform: scale(1); }

.prev,
.next { background: #8b1ba1; border-radius: 0; padding: 6px 15px 5px; }

.prev { border-radius: 25px 0 0 25px; }
.next { border-radius: 0 25px 25px 0; }
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
class Pagination {
constructor(container, config) {
const newConfig = config || {};

this.html = '';
this.total = newConfig.total || 10;
this.page = newConfig.page || 1;
this.step = newConfig.step || 3;

this.container = container;
this.list = container.querySelector('.pagination-list');
this.prev = container.querySelector('.prev');
this.next = container.querySelector('.next');
// start - example
this.main = document.querySelector('[data-js="main"]');
// end - example
this.handleClick = this.handleClick.bind(this);
this.handlePrevClick = this.handlePrevClick.bind(this);
this.handleNextClick = this.handleNextClick.bind(this);

this.render();
}

renderFirst() {
return '<li class="pagination-item"><button class="pagination-button">1</button></li><li class="pagination-item"><span>...</span></li>';
}

renderItems(begin, end) {
let items = '';
for (let i = begin; i < end; i++) {
items += '<li class="pagination-item"><button class="pagination-button">' + i + '</button></li>';
}

return items;
}

renderLast() {
return '<li class="pagination-item"><span>...</span></li><li class="pagination-item"><button class="pagination-button">' + this.total + '</button></li>';
}

handleClick(evt) {
this.page = parseInt(evt.target.innerText);
// start - example
this.main.innerHTML = this.page;
// end - example
this.render();
}

handlePrevClick() {
this.page--;

if (this.page < 1) {
this.page = 1;
}

this.render();
}

handleNextClick() {
this.page++;

if (this.page > this.total) {
this.page = this.total;
}

this.render();
}

bind() {
const items = this.list.querySelectorAll('.pagination-button');

this.prev.addEventListener('click', this.handlePrevClick);
this.next.addEventListener('click', this.handleNextClick);

for (let i = 0; i < items.length; i++) {
if (parseInt(items[i].innerText) === this.page) items[i].classList.add('active');
items[i].addEventListener('click', this.handleClick);
}
}

render() {
// minimun: total = 10, step = 1
if (this.total < this.step * 2 + 9) {
this.html += this.renderItems(1, this.total + 1);
}
// ex: total = 20, page = 4 , step = 2; 4 < 5
else if (this.page < this.step * 2 + 1) {
this.html += this.renderItems(1, this.step * 2 + 4);
this.html += this.renderLast();
}
// ex: total = 20, page = 17 , step = 2; === 17 > 16
else if (this.page > this.total - this.step * 2) {
this.html += this.renderFirst();
this.html += this.renderItems(this.total - this.step * 2 - 2, this.total + 1);
}
// ex: total = 20, page = 5,6,7,8,9,10,11,12,13,14,15,16 , step = 2;
else {
this.html += this.renderFirst();
this.html += this.renderItems(this.page - this.step, this.page + this.step + 1);
this.html += this.renderLast();
}
// start - example
this.main.innerHTML = this.page;
// end - example
this.list.innerHTML = this.html;
this.html = '';
this.bind();
}
};

const thirtyEight = () => {
new Pagination(document.querySelector('[data-js="pagination"]'), { total: 40, page: 1, step: 2 });
};

document.addEventListener('DOMContentLoaded', thirtyEight);

Conclusão

A postagem original pode ser vista no meu Instagram e o código está acessível no meu Github.

Relacionados

Ao fechar este aviso ou continuar navegando no site Nerd Calistênico, você aceita o uso de cookies.

Este site usa cookies para assegurar a melhor experiência para os nossos usuários. Consulte nossa política de privacidade.

Uma nova versão está disponível. Clique aqui para atualizar.