Back
Unveil the enchanting kinetic fan button that whirls visually like a captivating desk fan, inviting user engagement through movement and flair.
Welcome to this tutorial where you will learn how to create a kinetic fan button that visually spins like a mini desk fan. This button is designed to attract user engagement through movement and a playful design, embracing the tactile motion trend in modern web design.
First, we need to set up our HTML structure. We'll begin by defining a container for our button and apply a class of fan-container to center our content.
<div class="fan-container">
<button onclick="toggleFan()" class="kinetic-fan-button" aria-label="Toggle Fan Button">
<div class="fan-blades"></div>
</button>
</div>
The aria-label ensures accessibility by providing a descriptive label for screen readers. The inline JavaScript function toggleFan() handles the button's interaction.
Now, let's style our button to look like a fan and rotate its blades. We use Flexbox in the fan-container for center alignment and make our button circular with additional style that makes it appear as a fan.
.fan-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.kinetic-fan-button {
border: none;
padding: 20px;
border-radius: 50%;
background: #ffffff;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
cursor: pointer;
outline: none;
transition: transform 0.5s;
}
.fan-blades {
width: 40px;
height: 40px;
background: linear-gradient(90deg, transparent, #007bff, transparent);
clip-path: circle(40% at center);
transform: rotate(45deg);
}
By applying the fan-rotate class to kinetic-fan-button, we activate a CSS-only spin animation:
.fan-rotate {
animation: spin 1s infinite linear;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
This rotation uses a CSS keyframe animation, allowing seamless and reusable animation within the button.
Our JavaScript code manages the button's toggle state. We toggle the rotation animation by adding or removing the fan-rotate class when the button is activated:
let fanOn = false;
function toggleFan() {
let fanButton = document.querySelector('.kinetic-fan-button');
fanOn = !fanOn;
if (fanOn) {
fanButton.classList.add('fan-rotate');
} else {
fanButton.classList.remove('fan-rotate');
}
}
We use fanOn to keep track of whether the fan is currently spinning, and switch the class accordingly.
Congratulations! You've successfully built a kinetic fan button that combines playful interactions with a visually appealing design. This interactive component not only serves a functional purpose but transforms the user experience with animation and innovation.
Remember to keep user accessibility in mind by using descriptive labels and ensuring keyboard navigability in every interactive element you create. Happy coding!
<div class="fan-container">
<button onclick="toggleFan()" class="kinetic-fan-button" aria-label="Toggle Fan Button">
<div class="fan-blades"></div>
</button>
</div>
<script>
let fanOn = false;
function toggleFan() {
let fanButton = document.querySelector('.kinetic-fan-button');
fanOn = !fanOn;
if (fanOn) {
fanButton.classList.add('fan-rotate');
} else {
fanButton.classList.remove('fan-rotate');
}
}
</script>
<style>
.fan-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.kinetic-fan-button {
border: none;
padding: 20px;
border-radius: 50%;
background: #ffffff;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
cursor: pointer;
outline: none;
transition: transform 0.5s;
}
.fan-blades {
width: 40px;
height: 40px;
background: linear-gradient(90deg, transparent, #007bff, transparent);
clip-path: circle(40% at center);
transform: rotate(45deg);
}
.fan-rotate {
animation: spin 1s infinite linear;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
</style>
Thank you for reading this article.
Comments