Back
Unravel the mystery of a loader design that seamlessly fits puzzle pieces together, captivating users with engaging animation.
In this tutorial, we will create a visually captivating puzzle piece loader that adds charm and functionality to your website. This component utilizes a clever design to fit puzzle pieces together, signifying the progress of a loading task. Let's explore how to make it step-by-step.
We'll begin by setting up a simple HTML structure that forms the skeleton of our loader. This includes a container to hold the puzzle pieces.
<div class="puzzle-loader-container" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-live="polite"> <div class="puzzle-piece" aria-label="Loading piece 1"></div> <div class="puzzle-piece" aria-label="Loading piece 2"></div> <div class="puzzle-piece" aria-label="Loading piece 3"></div> <div class="puzzle-piece" aria-label="Loading piece 4"></div> </div>
We've chosen the <div> tag to structure our loader. Using role="progressbar" improves accessibility by conveying its purpose to assistive technologies. The aria-valuemin and aria-valuemax attributes define the range of our progress bar, while aria-live="polite" ensures updates do not interrupt user experience.
Next, we add styles to give our loader its puzzle-like appearance. Our CSS focuses on creating a seamless integration of puzzle pieces when they are fitted.
.puzzle-loader-container {
display: flex;
justify-content: center;
align-items: center;
gap: 5px;
padding: 20px;
}
.puzzle-piece {
width: 50px;
height: 50px;
background: #f0f0f0;
border-radius: 8px;
transition: transform 1s ease-in-out, background-color 0.5s ease;
box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.1);
}
.piece-fitted {
background-color: #81c784;
transform: rotate(360deg);
box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}
The .puzzle-loader-container class centers the puzzle pieces using Flexbox, making alignment straightforward. Each .puzzle-piece resembles a part of the puzzle. The transition property animates the transformation and color change, enhancing the effect of fitting the pieces together.
Here, we utilize JavaScript to animate the fitting of each puzzle piece. This involves sequentially adding and removing a class to trigger the puzzle-solving animation.
const puzzleContainer = document.querySelector('.puzzle-loader-container');
let currentPiece = 0;
setInterval(() => {
const pieces = document.querySelectorAll('.puzzle-piece');
pieces[currentPiece].classList.add('piece-fitted');
currentPiece = (currentPiece + 1) % pieces.length;
}, 1000);
In this block, we use setInterval to delay actions occurring over time. Every second, the piece-fitted class is applied to the next puzzle piece, triggering its animation.
With these three simple steps, we've crafted a playful and engaging puzzle piece loader. Such unique loaders not only serve functional purposes but also enhance user experience through delightful visual cues. Don't forget to implement accessibility guidelines, like aria-label, to enhance usability for all users.
<div class="puzzle-loader-container" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-live="polite">
<div class="puzzle-piece" aria-label="Loading piece 1"></div>
<div class="puzzle-piece" aria-label="Loading piece 2"></div>
<div class="puzzle-piece" aria-label="Loading piece 3"></div>
<div class="puzzle-piece" aria-label="Loading piece 4"></div>
</div>
<script>
const puzzleContainer = document.querySelector('.puzzle-loader-container');
let currentPiece = 0;
setInterval(() => {
const pieces = document.querySelectorAll('.puzzle-piece');
pieces[currentPiece].classList.add('piece-fitted');
currentPiece = (currentPiece + 1) % pieces.length;
}, 1000);
</script>
<style>
.puzzle-loader-container {
display: flex;
justify-content: center;
align-items: center;
gap: 5px;
padding: 20px;
}
.puzzle-piece {
width: 50px;
height: 50px;
background: #f0f0f0;
border-radius: 8px;
transition: transform 1s ease-in-out, background-color 0.5s ease;
box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.1);
}
.piece-fitted {
background-color: #81c784;
transform: rotate(360deg);
box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}
</style>
Thank you for reading this article.
Comments
No comments yet. Be the first to comment!