Back
Discover how to weave an ancient time-telling aesthetic into a modern toggle button that transitions between day and night modes with appealing visuals.
<div class="sundial-container">
<button onclick="toggleSundialTime()" class="toggle-sundial-button" aria-label="Toggle Sundial Time">
<span class="sundial-state">Day Mode</span>
</button>
</div>
toggleSundialTime changes button styling and alters the textual cue for users.
let isDay = true;
function toggleSundialTime() {
const sundialButton = document.querySelector('.toggle-sundial-button');
const stateText = document.querySelector('.sundial-state');
if (isDay) {
sundialButton.style.background = 'radial-gradient(circle, rgba(255,223,186,1) 0%, rgba(255,197,61,1) 100%)';
stateText.textContent = 'Night Mode';
sundialButton.classList.add('night');
} else {
sundialButton.style.background = 'radial-gradient(circle, rgba(54,162,235,1) 0%, rgba(153,217,234,1) 100%)';
stateText.textContent = 'Day Mode';
sundialButton.classList.remove('night');
}
isDay = !isDay;
}
.sundial-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.toggle-sundial-button {
border: none;
padding: 20px 40px;
border-radius: 50px;
color: white;
font-size: 18px;
font-weight: bold;
transition: background 0.3s, box-shadow 0.3s, transform 0.3s;
cursor: pointer;
background: radial-gradient(circle, rgba(54,162,235,1) 0%, rgba(153,217,234,1) 100%);
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}
The .night class introduces rotational animations, pivoting the button like a sundial. This enriches the tactile sensation, providing a fun, visual interaction with smooth rotations and enhanced shadows.
.night {
transform: rotate(360deg);
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.5);
}
As we've explored, this Sundial Time Toggle Button is not merely a functional component but a piece of interactive whimsy, guiding users through time in a captivating manner.
<div class="sundial-container">
<button onclick="toggleSundialTime()" class="toggle-sundial-button" aria-label="Toggle Sundial Time">
<span class="sundial-state">Day Mode</span>
</button>
</div>
<script>
let isDay = true;
function toggleSundialTime() {
const sundialButton = document.querySelector('.toggle-sundial-button');
const stateText = document.querySelector('.sundial-state');
if (isDay) {
sundialButton.style.background = 'radial-gradient(circle, rgba(255,223,186,1) 0%, rgba(255,197,61,1) 100%)';
stateText.textContent = 'Night Mode';
sundialButton.classList.add('night');
} else {
sundialButton.style.background = 'radial-gradient(circle, rgba(54,162,235,1) 0%, rgba(153,217,234,1) 100%)';
stateText.textContent = 'Day Mode';
sundialButton.classList.remove('night');
}
isDay = !isDay;
}
</script>
<style>
.sundial-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.toggle-sundial-button {
border: none;
padding: 20px 40px;
border-radius: 50px;
color: white;
font-size: 18px;
font-weight: bold;
transition: background 0.3s, box-shadow 0.3s, transform 0.3s;
cursor: pointer;
background: radial-gradient(circle, rgba(54,162,235,1) 0%, rgba(153,217,234,1) 100%);
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}
.night {
transform: rotate(360deg);
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.5);
}
</style>
Thank you for reading this article.
Comments
No comments yet. Be the first to comment!