Back

Crafting an Enchanting Floating Book Loader

Learn how to create a loader that mimics a levitating book with pages flipping, revealing the beauty of reading.

Component Explanation

Crafting an Enchanting Floating Book Loader

The Floating Book Loader is an innovative take on loading animations that brings the magic of reading to your web pages. This loader showcases a book with pages that appear to turn, inviting users into a world of imagination and discovery. In this tutorial, we’ll guide you through creating this delightful effect using HTML, CSS, and JavaScript. Let’s begin!

HTML

We'll start by defining the structure of the loader in HTML. A <main> element houses our floating book component, which consists of a book cover and pages that turn. Here, semantic HTML is crucial for SEO, while aria-label attributes ensure accessibility: they provide descriptive labels for users relying on screen readers.


        <main class="floating-book-container">
          <section class="book-loader" aria-label="Loading">
            <div class="book-cover" role="img" aria-label="A floating book with turning pages"></div>
            <div class="pages"><div class="page" aria-hidden="true"></div><div class="page" aria-hidden="true"></div><div class="page" aria-hidden="true"></div></div>
          </section>
        </main>
      

CSS

With the structure in place, we move on to styling with CSS. We use flexbox properties in the .floating-book-container to position elements centrally. The .book-cover and .page classes create the visual elements of the book, leveraging properties like box-shadow and background gradients to enhance the aesthetics. Animations are pivotal; we use the transform and transition properties to create the flip effect, but ensure these are optional for users with reduced motion preferences.


        .floating-book-container {
          display: flex;
          justify-content: center;
          align-items: center;
          height: 100vh;
        }
        .book-loader {
          width: 100px;
          height: 80px;
          position: relative;
          perspective: 1000px;
        }
        .book-cover {
          width: 100%;
          height: 100%;
          background: linear-gradient(45deg, #d1c4e9, #b39ddb);
          box-shadow: 0 15px 35px rgba(0,0,0,0.2);
          border-radius: 5px;
        }
        .pages {
          position: absolute;
          width: 100%;
          height: 100%;
          top: 0;
          left: 0;
          transform-style: preserve-3d;
        }
        .page {
          width: 100%;
          height: 100%;
          background: white;
          border-radius: 5px;
          position: absolute;
          box-shadow: 0 5px 15px rgba(0,0,0,0.1);
          transform: rotateY(0deg) translateZ(1px);
          transition: transform 0.6s;
        }
        .page.active {
          transform: rotateY(-180deg) translateZ(1px);
        }
      

JavaScript

JavaScript animates the loader by cycling through pages periodically. We ensure it's sensitive to user settings by checking prefers-reduced-motion to disable animations if necessary. This script toggles the active class on each page element, enacting the flip motion. As accessibility best practices, keyboard accessibility and clear labeling are essential to prioritize usability for all users.


        const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
        if (!prefersReducedMotion) {
          let pageIndex = 0;
          setInterval(() => {
            const pages = document.querySelectorAll('.page');
            pages.forEach(page => page.classList.remove('active'));
            pages[pageIndex].classList.add('active');
            pageIndex = (pageIndex + 1) % pages.length;
          }, 1000);
        }
      

Conclusion

The magic of the Floating Book Loader doesn't only lie in its visual appeal but also in its thoughtful design that keeps accessibility and user preferences front and center. This ensures a fantastic user experience across all devices and abilities. With this tutorial, you've expanded your UI/UX horizons, crafting an experience that moves beyond simple loading indicators into delightful tales told through code. Happy coding and storytelling!

Whole code

<main class="floating-book-container">
  <section class="book-loader" aria-label="Loading">
    <div class="book-cover" role="img" aria-label="A floating book with turning pages"></div>
    <div class="pages"><div class="page" aria-hidden="true"></div><div class="page" aria-hidden="true"></div><div class="page" aria-hidden="true"></div></div>
  </section>
</main>


<script>
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!prefersReducedMotion) {
  let pageIndex = 0;
  setInterval(() => {
    const pages = document.querySelectorAll('.page');
    pages.forEach(page => page.classList.remove('active'));
    pages[pageIndex].classList.add('active');
    pageIndex = (pageIndex + 1) % pages.length;
  }, 1000);
}
</script>

<style>
.floating-book-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.book-loader {
  width: 100px;
  height: 80px;
  position: relative;
  perspective: 1000px;
}
.book-cover {
  width: 100%;
  height: 100%;
  background: linear-gradient(45deg, #d1c4e9, #b39ddb);
  box-shadow: 0 15px 35px rgba(0,0,0,0.2);
  border-radius: 5px;
}
.pages {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  transform-style: preserve-3d;
}
.page {
  width: 100%;
  height: 100%;
  background: white;
  border-radius: 5px;
  position: absolute;
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
  transform: rotateY(0deg) translateZ(1px);
  transition: transform 0.6s;
}
.page.active {
  transform: rotateY(-180deg) translateZ(1px);
}
</style>
      
Thank you for reading this article.

Comments

More loaders

Similar

See also