Back
Discover a unique avatar component that morphs through various historical eras, offering a dynamic and entertaining representation.
In this tutorial, we dive into the creation of a unique time-traveling avatar component. This avatar seamlessly transitions through different historical eras, using a playful blend of HTML, CSS, and JavaScript. Our design allows the avatar to change its appearance with the click of a button, giving users a delightful and interactive experience.
The HTML structure of our component consists of a container with a button. This button contains an image and a descriptive text area that announces the current era of the avatar. By using semantic HTML tags, we ensure that our component is accessible and meaningful to screen readers.
<div class="time-travel-avatar-container">
<button aria-label="Change Avatar Era" onclick="changeEra()" class="avatar-switch-button">
<img src="https://randomuser.me/api/portraits/lego/1.jpg" alt="Current Avatar" class="avatar-image" />
</button>
<span class="era-description">Present Era</span>
</div>
The aria-label attribute improves accessibility by clearly explaining what the button does. The avatar image is adaptable, dynamically switching to represent a different period with each click.
Our JavaScript manages the transition between the different avatars representing historical eras. By declaring an array of eras, each containing an image and description, we create a looping functionality that updates both the avatar and the era text. This logic is handled by the changeEra function.
let currentIndex = 0;
const eras = [
{ image: 'https://randomuser.me/api/portraits/lego/2.jpg', description: 'Ancient Egypt' },
{ image: 'https://randomuser.me/api/portraits/lego/3.jpg', description: 'Victorian Era' },
{ image: 'https://randomuser.me/api/portraits/lego/4.jpg', description: 'Roaring Twenties' },
{ image: 'https://randomuser.me/api/portraits/lego/5.jpg', description: 'Future' }
];
function changeEra() {
currentIndex = (currentIndex + 1) % eras.length;
const avatarImage = document.querySelector('.avatar-image');
const eraDescription = document.querySelector('.era-description');
avatarImage.src = eras[currentIndex].image;
eraDescription.textContent = eras[currentIndex].description;
}
The changeEra function cycles through the avatar images and their corresponding descriptions. Each time the button is clicked, the currentIndex variable updates to the next era, looping back to the beginning after reaching the end of the list.
In the styling phase, we focus on creating an engaging yet simple visual experience. The layout is centered, and interaction effects are added to enhance the button's responsiveness. Key styles such as rounded avatar images and smooth transitions help create a modern, dynamic look.
.time-travel-avatar-container {
display: flex;
flex-direction: column;
align-items: center;
margin: 20px;
text-align: center;
}
.avatar-switch-button {
background: transparent;
border: none;
cursor: pointer;
transition: transform 0.4s ease, box-shadow 0.3s ease;
}
.avatar-switch-button:focus {
outline: 2px solid #ffcc00;
}
.avatar-image {
width: 120px;
height: 120px;
border-radius: 50%;
transition: transform 0.5s ease;
}
.avatar-switch-button:hover .avatar-image {
transform: rotate(360deg);
}
.era-description {
margin-top: 10px;
font-size: 18px;
color: #555;
}
Hover and focus effects provide visual feedback, making the component interactive and accessible. The hover effect on the image delivers a pleasant surprise by animating it with rotation, adding a delightful layer to the user's interaction.
The Time Travel Avatar is more than just a representation of faces—it's an interactive journey through time. From ancient civilizations to the uncharted future, this component is perfect for projects that are looking to add creative flair. By following this guide, you've not just built a UI feature, but also a fun little history ride!
Keep experimenting with this example and add more eras to capture more history and future possibilities. Enjoy unleashing creativity with ever-expanding possibilities!
<div class="time-travel-avatar-container">
<button aria-label="Change Avatar Era" onclick="changeEra()" class="avatar-switch-button">
<img src="https://randomuser.me/api/portraits/lego/1.jpg" alt="Current Avatar" class="avatar-image" />
</button>
<span class="era-description">Present Era</span>
</div>
<script>
let currentIndex = 0;
const eras = [
{ image: 'https://randomuser.me/api/portraits/lego/2.jpg', description: 'Ancient Egypt' },
{ image: 'https://randomuser.me/api/portraits/lego/3.jpg', description: 'Victorian Era' },
{ image: 'https://randomuser.me/api/portraits/lego/4.jpg', description: 'Roaring Twenties' },
{ image: 'https://randomuser.me/api/portraits/lego/5.jpg', description: 'Future' }
];
function changeEra() {
currentIndex = (currentIndex + 1) % eras.length;
const avatarImage = document.querySelector('.avatar-image');
const eraDescription = document.querySelector('.era-description');
avatarImage.src = eras[currentIndex].image;
eraDescription.textContent = eras[currentIndex].description;
}
</script>
<style>
.time-travel-avatar-container {
display: flex;
flex-direction: column;
align-items: center;
margin: 20px;
text-align: center;
}
.avatar-switch-button {
background: transparent;
border: none;
cursor: pointer;
transition: transform 0.4s ease, box-shadow 0.3s ease;
}
.avatar-switch-button:focus {
outline: 2px solid #ffcc00;
}
.avatar-image {
width: 120px;
height: 120px;
border-radius: 50%;
transition: transform 0.5s ease;
}
.avatar-switch-button:hover .avatar-image {
transform: rotate(360deg);
}
.era-description {
margin-top: 10px;
font-size: 18px;
color: #555;
}
</style>
Thank you for reading this article.
Comments
No comments yet. Be the first to comment!