Back

Reimagining Pagination as a Kaleidoscope-Inspired Experience

Dive into the creation of a circular pagination component that mesmerizes users with a kaleidoscope effect, weaving modern UI trends into classic navigation functionality.

Component Explanation

Creating a Kaleidoscope-Inspired Circular Pagination Component

In this blog post, we'll journey through the art of transforming traditional pagination into a kaleidoscope of interaction and design. Inspired by the ever-fascinating kaleidoscope, our pagination feature will be a dynamic, colorful ring of navigation that captivates users with each interaction.

HTML: Structuring the Kaleidoscope Pagination

The heart of any pagination component lies in its HTML structure. Here, we use semantic HTML to ensure accessibility and good SEO practices. Our pagination component is wrapped inside a <nav> element with an aria-label for screen readers, offering a clear description of its purpose.


<nav aria-label="Kaleidoscope pagination" class="kaleidoscope-pagination">
  <ul class="pagination-circles">
    <li tabindex="0" aria-label="Page 1" class="page-item active" onclick="changePage(1)"></li>
    <li tabindex="0" aria-label="Page 2" class="page-item" onclick="changePage(2)"></li>
    <li tabindex="0" aria-label="Page 3" class="page-item" onclick="changePage(3)"></li>
    <li tabindex="0" aria-label="Page 4" class="page-item" onclick="changePage(4)"></li>
    <li tabindex="0" aria-label="Page 5" class="page-item" onclick="changePage(5)"></li>
  </ul>
</nav>

The ul element holds the pagination items, and each list item represents a page. We ensure keyboard accessibility with tabindex and give clear instructions with aria-label attributes for each page.

JavaScript: Bringing Pages to Life

The toggle effect for our pagination is handled by JavaScript. The function changePage() updates the active page visually and is triggered on clicking a pagination circle.


function changePage(pageNum) {
  let items = document.querySelectorAll('.page-item');
  items.forEach(item => item.classList.remove('active'));
  let activeItem = document.querySelector('.page-item:nth-child(' + pageNum + ')');
  activeItem.classList.add('active');
}

This code snippet loops through each pagination item, ensuring to remove the active class and then applies it to the selected item based on pageNum. This way, only one item is active at a time.

CSS: Styling the Kaleidoscope Experience

The vibrant transformation occurs primarily in the CSS. We use radial-gradient to simulate a kaleidoscope effect when a page is active.


.kaleidoscope-pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}
.pagination-circles {
  list-style: none;
  display: flex;
  gap: 10px;
  padding: 0;
}
.page-item {
  width: 20px;
  height: 20px;
  background: radial-gradient(circle, rgba(135,206,235,0.8), rgba(30,144,255,0.6));
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.3s, background 0.3s;
  outline: none;
}
.page-item:focus {
  box-shadow: 0 0 0 3px rgba(30,144,255,0.6);
}
.page-item.active {
  transform: scale(1.3);
  background: radial-gradient(circle, rgba(255,182,193,0.8), rgba(255,105,180,0.6));
  box-shadow: 0 0 10px rgba(255,182,193,0.6);
}

The .page-item class is responsible for each pagination circle's default style: a pleasing gradient effect. When the .active class is triggered, the style enriches, scaling up with a brighter gradient and shadow, providing a compelling visual cue of the current page.

Optimizing for Accessibility and Preferences

We've ensured keyboard navigation by including tabindex on each page item. The focus state uses a box-shadow for a clearer indication when items are navigated without a mouse. As a modern touch, our design respects user preferences for reduced motion, by facilitating motion via CSS transforms which can be universally disabled.

With this inventive approach, pagination morphs beyond a utility, blossoming into a visual delight, symbolic of how straightforward elements can engage users through imaginative execution.

Whole code

<nav aria-label="Kaleidoscope pagination" class="kaleidoscope-pagination">
  <ul class="pagination-circles">
    <li tabindex="0" aria-label="Page 1" class="page-item active" onclick="changePage(1)"></li>
    <li tabindex="0" aria-label="Page 2" class="page-item" onclick="changePage(2)"></li>
    <li tabindex="0" aria-label="Page 3" class="page-item" onclick="changePage(3)"></li>
    <li tabindex="0" aria-label="Page 4" class="page-item" onclick="changePage(4)"></li>
    <li tabindex="0" aria-label="Page 5" class="page-item" onclick="changePage(5)"></li>
  </ul>
</nav>


<script>
function changePage(pageNum) {
  let items = document.querySelectorAll('.page-item');
  items.forEach(item => item.classList.remove('active'));
  let activeItem = document.querySelector('.page-item:nth-child(' + pageNum + ')');
  activeItem.classList.add('active');
}
</script>

<style>
.kaleidoscope-pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}
.pagination-circles {
  list-style: none;
  display: flex;
  gap: 10px;
  padding: 0;
}
.page-item {
  width: 20px;
  height: 20px;
  background: radial-gradient(circle, rgba(135,206,235,0.8), rgba(30,144,255,0.6));
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.3s, background 0.3s;
  outline: none;
}
.page-item:focus {
  box-shadow: 0 0 0 3px rgba(30,144,255,0.6);
}
.page-item.active {
  transform: scale(1.3);
  background: radial-gradient(circle, rgba(255,182,193,0.8), rgba(255,105,180,0.6));
  box-shadow: 0 0 10px rgba(255,182,193,0.6);
}

</style>
      
Thank you for reading this article.

Comments

More paginations

Similar

See also