Back
Explore how to create a dynamically transforming toggle button inspired by a kaleidoscope that visually shifts through mesmerizing colors with each toggle state.
<div class="kaleidoscope-container">
<button onclick="blendColors()" class="toggle-blend-button">
<span class="toggle-state">OFF</span>
</button>
</div>
blendColors function switches this state and dynamically updates the button's background gradient and text accordingly.
let toggled = false;
function blendColors() {
let button = document.querySelector('.toggle-blend-button');
let stateText = document.querySelector('.toggle-state');
toggled = !toggled;
if (toggled) {
button.style.background = "linear-gradient(135deg, rgba(255,200,0,0.9), rgba(255,100,150,0.9))";
stateText.textContent = "ON";
button.classList.add('blend-on');
} else {
button.style.background = "linear-gradient(135deg, rgba(0,200,255,0.9), rgba(0,100,150,0.9))";
stateText.textContent = "OFF";
button.classList.remove('blend-on');
}
}
.kaleidoscope-container class sets a visually pleasing backdrop for central alignment. Alignments are easily managed using flexbox.
.kaleidoscope-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
The .toggle-blend-button class is the heart, where the magic happens. By default, the button boasts a simple gradient that intriguingly adapts when toggled, thanks to a harmonious transition effect.
.toggle-blend-button {
border: none;
padding: 15px 30px;
border-radius: 50px;
color: white;
font-size: 18px;
font-weight: bold;
transition: background 0.5s, transform 0.3s;
cursor: pointer;
background: linear-gradient(135deg, rgba(0,200,255,0.9), rgba(0,100,150,0.9));
}
The .blend-on class enhances interactivity by amplifying button presentation with scale and shadow effects, mimicking a delightful kaleidoscope shimmer.
.blend-on {
transform: scale(1.1);
box-shadow: 0 0 15px rgba(255,200,0,0.7), 0 0 20px rgba(255,100,150,0.7);
}
And thus, we've crafted a button that not only serves functionality but also adds delightful visual intrigue.
<div class="kaleidoscope-container">
<button onclick="blendColors()" class="toggle-blend-button">
<span class="toggle-state">OFF</span>
</button>
</div>
<script>
let toggled = false;
function blendColors() {
let button = document.querySelector('.toggle-blend-button');
let stateText = document.querySelector('.toggle-state');
toggled = !toggled;
if (toggled) {
button.style.background = "linear-gradient(135deg, rgba(255,200,0,0.9), rgba(255,100,150,0.9))";
stateText.textContent = "ON";
button.classList.add('blend-on');
} else {
button.style.background = "linear-gradient(135deg, rgba(0,200,255,0.9), rgba(0,100,150,0.9))";
stateText.textContent = "OFF";
button.classList.remove('blend-on');
}
}
</script>
<style>
.kaleidoscope-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.toggle-blend-button {
border: none;
padding: 15px 30px;
border-radius: 50px;
color: white;
font-size: 18px;
font-weight: bold;
transition: background 0.5s, transform 0.3s;
cursor: pointer;
background: linear-gradient(135deg, rgba(0,200,255,0.9), rgba(0,100,150,0.9));
}
.blend-on {
transform: scale(1.1);
box-shadow: 0 0 15px rgba(255,200,0,0.7), 0 0 20px rgba(255,100,150,0.7);
}
</style>
Thank you for reading this article.
Comments
No comments yet. Be the first to comment!