Pagination
by phloe
HTML
<ol class="paging">
<li class="current">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
</ol>
<ol class="paging">
<li>1</li>
<li class="current">2</li>
</ol>
<ol class="paging">
<li>1</li>
<li>2</li>
<li>3</li>
<li class="current">4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
</ol>
<ol class="paging">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li class="current">9</li>
</ol>
CSS
ol {
display: block;
margin: 0 0 40px 60px;
padding: 0;
width: 75px;
position: relative;
height: 15px;
user-select: none;
}
li {
position: absolute;
top: 0;
left: 0;
display: inline-block;
width: 15px;
height: 15px;
background-color: rgba(0, 0, 0, 0.99);
margin-left: 15px;
clip-path: inset(0 round 7.5px);
color: transparent;
transition: transform 0.2s ease-in-out, background-color 0.2s ease-in-out, opacity 0.2s ease-in-out, width 0.2s ease-in-out;
opacity: 0;
transform: translateX(-60px);
cursor: pointer;
}
li:after {
position: absolute;
top: -10px;
left: -10px;
bottom: -10px;
right: -10px;
content: "";
display: block;
border-radius: 50%;
}
li:hover {
background-color: #00bac7;
}
.previous--second {
transform: translateX(-30px);
opacity: 0.5;
}
.previous--first {
transform: translateX(0);
opacity: 1;
}
.current {
transform: translateX(30px);
opacity: 1;
cursor: default;
width: 45px;
}
.current:hover {
background-color: #db001a;
}
.next--first {
transform: translateX(90px);
opacity: 1;
}
.next--second {
transform: translateX(120px);
opacity: 0.5;
}
.next--second ~ li {
transform: translateX(150px);
opacity: 0;
}
JavaScript
const states = ["previous--second", "previous--first", "current", "next--first", "next--second"];
[...document.querySelectorAll(".paging")].forEach(
(container) => {
const items = [...container.querySelectorAll("li")];
container.addEventListener("click", (event) => {
const { target } = event;
if (
container !== target
&& target.nodeName.toLowerCase() === "li"
) {
switchCurrent(target, true);
return false;
}
});
let currentIndex;
var current = container.querySelector(".current");
switchCurrent(current);
function switchCurrent(el) {
const newIndex = items.indexOf(el);
if (newIndex !== currentIndex) {
states.forEach((state, stateIndex) => {
let itemIndex;
if (typeof currentIndex === "number") {
itemIndex = currentIndex + (stateIndex - 2);
if (itemIndex in items) {
items[itemIndex].classList.remove(state);
}
}
itemIndex = newIndex + (stateIndex - 2);
if (itemIndex in items) {
items[itemIndex].classList.add(state);
}
});
currentIndex = newIndex;
}
}
}
);