Back
Dive into the creation of an interactive tooltip emulating a time capsule that dramatically reveals hidden information through a gentle time fade.
Tooltips are indispensable for providing additional information to users without cluttering the UI. Imagine a tooltip that not only shares information but also provides an engaging user experience with the illusion of unveiling a secret from a hidden time capsule. In this tutorial, we'll explore creating a Time Capsule Tooltip that employs a seamless opacity transition to reveal information, resembling the gentle opening of a time capsule.
First, we set up our HTML structure with semantic roles and attributes for accessibility:
<div class="time-capsule-container">
<button aria-label="Open Time Capsule" class="tooltip-button">
Hover me
</button>
<div class="tooltip-content" role="tooltip">
<p>This is a secret from the past!</p>
</div>
</div>
Here, we use a <button> element with an aria-label attribute to ensure it is accessible to screen readers. The .tooltip-content div has a role="tooltip" to convey its purpose semantically.
Our CSS aims to leverage modern design trends, like glassmorphism, to create a visually compelling experience:
.time-capsule-container {
position: relative;
display: inline-block;
text-align: center;
}
.tooltip-button {
background-color: #03a9f4;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
transition: transform 0.3s ease;
}
.tooltip-button:focus {
outline: 2px solid #fff;
}
.tooltip-content {
visibility: hidden;
width: 200px;
background-color: rgba(255,255,255,0.8);
color: #333;
text-align: center;
padding: 10px;
border-radius: 10px;
position: absolute;
z-index: 1;
bottom: 120%;
left: 50%;
margin-left: -100px;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
transition: visibility 0s, opacity 0.3s ease;
opacity: 0;
}
.tooltip-content::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: rgba(255,255,255,0.8) transparent transparent transparent;
}
.show-tooltip {
visibility: visible;
opacity: 1;
}
The glassmorphism effect is achieved using a semi-transparent background with a subtle box shadow, creating the illusion of depth and mystery. The opacity transition allows for a silky reveal.
The interactive element involves adding some JavaScript to detect hover events:
document.querySelector('.tooltip-button').addEventListener('mouseenter', function() {
const content = document.querySelector('.tooltip-content');
content.classList.add('show-tooltip');
});
document.querySelector('.tooltip-button').addEventListener('mouseleave', function() {
const content = document.querySelector('.tooltip-content');
content.classList.remove('show-tooltip');
});
We apply event listeners for the 'mouseenter' and 'mouseleave' events on the button. When the user hovers, the show-tooltip class is toggled, altering the visibility and opacity of the tooltip content to produce a smooth transition. The toggle ensures that the tooltip both appears and disappears gracefully.
Ensuring our tooltip is accessible to all users is vital. We use aria-label for the button to be screen reader-friendly, and all animations take user preferences into account with the media query prefers-reduced-motion to disable animations if needed.
The Time Capsule Tooltip combines aesthetic allure with usability, presenting an intriguing tooltip design that delights users, elevating mundane information exchange to an enchanting experience. Whether you're building detailed data views or simple web apps, this tooltip will captivate and inform seamlessly.
<div class="time-capsule-container">
<button aria-label="Open Time Capsule" class="tooltip-button">
Hover me
</button>
<div class="tooltip-content" role="tooltip">
<p>This is a secret from the past!</p>
</div>
</div>
<script>
document.querySelector('.tooltip-button').addEventListener('mouseenter', function() {
const content = document.querySelector('.tooltip-content');
content.classList.add('show-tooltip');
});
document.querySelector('.tooltip-button').addEventListener('mouseleave', function() {
const content = document.querySelector('.tooltip-content');
content.classList.remove('show-tooltip');
});
</script>
<style>
.time-capsule-container {
position: relative;
display: inline-block;
text-align: center;
}
.tooltip-button {
background-color: #03a9f4;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
transition: transform 0.3s ease;
}
.tooltip-button:focus {
outline: 2px solid #fff;
}
.tooltip-content {
visibility: hidden;
width: 200px;
background-color: rgba(255,255,255,0.8);
color: #333;
text-align: center;
padding: 10px;
border-radius: 10px;
position: absolute;
z-index: 1;
bottom: 120%;
left: 50%;
margin-left: -100px;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
transition: visibility 0s, opacity 0.3s ease;
opacity: 0;
}
.tooltip-content::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: rgba(255,255,255,0.8) transparent transparent transparent;
}
.show-tooltip {
visibility: visible;
opacity: 1;
}
@media (prefers-reduced-motion: reduce) {
.tooltip-button:hover {
transform: none;
}
.tooltip-content {
transition: none;
}
}
</style>
Thank you for reading this article.
Comments
No comments yet. Be the first to comment!