Back

Crafting a Dynamic Fragmenting Puzzle Loader

Design an interactive piece-spinner loader that visually assimilates a puzzle solving itself during the loading process.

Component Explanation

Main Title: Create a Dynamic Fragmenting Puzzle Loader

HTML Layout

We start by defining a container that will house our puzzle loader—an animated and interactive set of elements visually representing a puzzle fragmenting and completing itself.
  <div class="puzzle-loader-container">
    <div class="puzzle-loader" aria-label="Puzzle Piece Loader" tabindex="0">
      <div class="puzzle-piece"></div>
      <div class="puzzle-piece"></div>
      <div class="puzzle-piece"></div>
      <div class="puzzle-piece"></div>
    </div>
  </div>

JavaScript Functionality

We use JavaScript primarily to stagger animations and detect user preferences for reduced motion. This ensures our loader is both dynamic and accessible.
  const enableReduction = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  if (!enableReduction) {
    let pieces = document.querySelectorAll('.puzzle-piece');
    pieces.forEach((piece, index) => {
      piece.style.animationDelay = `${index * 0.2}s`;
      piece.style.animationDuration = '1.5s';
    });
  }

CSS Stylings and Animations

The CSS implementations handle layout and animations. Grid layout allows seamless arrangement of pieces, while CSS animations provide interactivity and rotation.
  .puzzle-loader-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
  }

  .puzzle-loader {
    display: grid;
    grid-template-columns: repeat(2, 50px);
    grid-template-rows: repeat(2, 50px);
    gap: 10px;
    transform: rotate(0deg);
    animation: spin 2s linear infinite;
  }

  .puzzle-piece {
    width: 50px;
    height: 50px;
    background: rgba(173, 216, 230, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    animation: wobble 1.5s ease-in-out infinite;
  }

  @keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
  }

  @keyframes wobble {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    50% { transform: translateX(5px); }
    75% { transform: translateX(-5px); }
  }

  .puzzle-loader:focus-visible, .puzzle-piece:focus-visible {
    outline: 3px solid #ffbb00;
    outline-offset: 2px;
  }
The loader components ensure accessibility, visual flair, and interactivity within contemporary web applications.

Whole code

<div class="puzzle-loader-container">
  <div class="puzzle-loader" aria-label="Puzzle Piece Loader" tabindex="0">
    <div class="puzzle-piece"></div>
    <div class="puzzle-piece"></div>
    <div class="puzzle-piece"></div>
    <div class="puzzle-piece"></div>
  </div>
</div>

<script>
const enableReduction = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!enableReduction) {
  let pieces = document.querySelectorAll('.puzzle-piece');
  pieces.forEach((piece, index) => {
    piece.style.animationDelay = `${index * 0.2}s`;
    piece.style.animationDuration = '1.5s';
  });
}
</script>

<style>
.puzzle-loader-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.puzzle-loader {
  display: grid;
  grid-template-columns: repeat(2, 50px);
  grid-template-rows: repeat(2, 50px);
  gap: 10px;
  transform: rotate(0deg);
  animation: spin 2s linear infinite;
}
.puzzle-piece {
  width: 50px;
  height: 50px;
  background: rgba(173, 216, 230, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  animation: wobble 1.5s ease-in-out infinite;
}
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
@keyframes wobble {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  50% { transform: translateX(5px); }
  75% { transform: translateX(-5px); }
}
.puzzle-loader:focus-visible, .puzzle-piece:focus-visible {
  outline: 3px solid #ffbb00;
  outline-offset: 2px;
}
</style>
      
Thank you for reading this article.

Comments

No comments yet. Be the first to comment!

More loaders

Similar

See also