<div class="pagination">
<a href="#" class="arrow" id="left">
<i class="fas fa-angle-left"></i>
</a>
<a href="#" class="active">1</a>
<a href="#">2</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#" class="arrow" id="right">
<i class="fas fa-angle-right"></i>
</a>
</div>
body {
height: 100vh;
display: flex;
justify-content: center;
}
.pagination {
display: flex;
justify-content: center;
align-items: center;
}
.pagination a {
display: inline-block;
padding: 10px 15px;
margin: 0 5px;
background-color: #e6ddc4;
border-radius: 5px;
text-decoration: none;
color: #181d31;
font-weight: bold;
}
.pagination a:hover {
background-color: #181d31;
color: white;
}
.pagination i {
font-size: 18px;
}
.pagination .active {
background-color: #181d31;
color: white;
}
const paginationLinks = document.querySelectorAll(".pagination a");
let currentIndex = 0;
paginationLinks.forEach((link, index) => {
if (link.classList.contains("active")) {
currentIndex = index;
}
link.addEventListener("click", () => {
if (link.classList.contains("arrow")) {
if (link.id === "left") {
currentIndex = Math.max(currentIndex - 1, 0);
} else {
currentIndex = Math.min(currentIndex + 1, paginationLinks.length - 2);
}
} else {
currentIndex = index;
}
paginationLinks.forEach((link) => link.classList.remove("active"));
paginationLinks[currentIndex].classList.add("active");
});
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.