Back

Exploring the Sundial Timer Toggle Button

Dive into building a time-inspired toggle button that mimics a sundial, integrating the passage of time in its ever-rotating animation.

Component Explanation

Building an Animated Sundial Timer Toggle Button with CSS and JavaScript

Introduction: Time Telling with a Sundial Toggle

In this tutorial, we're going to design an interactive toggle button that draws inspiration from the ancient sundial. The Sundial Timer Toggle Button uniquely displays the transition from day to night, using a simple yet elegant rotation animation reflecting the passage of time. We'll explore how to implement this with HTML, CSS, and JavaScript, ensuring accessibility and interactivity throughout.

Step 1: Setting Up the HTML Structure with Accessibility in Mind

We'll start by creating a container for our sundial button. This container will center our element on the screen, making it the focal point for users.

<div class="sundial-container">
  <button onclick="toggleSun()" class="sundial-button" aria-label="Toggle Sundial Timer">
    <span class="sun-indicator">🌞</span>
  </button>
</div>

Our button employs aria-label to describe its function to screen readers, enhancing accessibility. The span inside the button visually represents the current state (day or night) with an intuitive icon.

Step 2: Crafting the CSS for Visual Appeal and Modern Design

Let's style our button to mimic a sundial. We'll use radial gradients and transitions to reflect time changes dynamically. Our standard state mimics daylight, transitioning to a night-themed design upon toggle activation.

.sundial-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.sundial-button {
  border: none;
  padding: 20px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 223, 0, 0.9) 0%, rgba(255, 165, 0, 0.8) 100%);
  color: rgba(255, 255, 255, 0.8);
  font-size: 24px;
  cursor: pointer;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 1s ease-in-out;
}
.sundial-on {
  transform: rotate(180deg);
  background: radial-gradient(circle, rgba(30, 30, 36, 0.9) 0%, rgba(85, 85, 112, 0.8) 100%);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}
.sun-indicator {
  transition: color 0.5s;
}

We apply radial-gradient backgrounds to simulate the lighting changes inherent to a sundial. Transitions ensure a smooth visual experience, rotating the button 180 degrees to represent the shift from day to night.

Step 3: Adding Interactive Logic with JavaScript

Our JavaScript will toggle the button state, switching visuals and captions between day and night representations. This script ensures our button reacts to user interaction intuitively.

let sunToggled = false;
function toggleSun() {
  let button = document.querySelector('.sundial-button');
  let sunIndicator = document.querySelector('.sun-indicator');
  sunToggled = !sunToggled;
  if (sunToggled) {
    button.classList.add('sundial-on');
    sunIndicator.textContent = "🌒";
  } else {
    button.classList.remove('sundial-on');
    sunIndicator.textContent = "🌞";
  }
}

The toggleSun function manages the toggle state, switching the sun icon based on the toggle status. The classes applied to the button adjust its presentation, visually reflecting whether it's night or day.

Conclusion: Crafting a Functionally Aesthetic Component

This tutorial guided us through creating an interactive toggle button based on sundial aesthetics. We've seamlessly blended historical timekeeping elements with modern web design trends, resulting in a component that's both functional and visually captivating. This button exemplifies how we can draw inspiration from everyday objects to design innovative user interfaces.

As you integrate this component into your projects, remember to test accessibility features to ensure usability for all audiences. By combining semantic HTML, CSS transitions, and intuitive JavaScript logic, our Sundial Timer Toggle stands as a testament to the boundless possibilities in UI design.

Whole code

<div class="sundial-container">
  <button onclick="toggleSun()" class="sundial-button" aria-label="Toggle Sundial Timer">
    <span class="sun-indicator">🌞</span>
  </button>
</div>

<script>
let sunToggled = false;
function toggleSun() {
  let button = document.querySelector('.sundial-button');
  let sunIndicator = document.querySelector('.sun-indicator');
  sunToggled = !sunToggled;
  if (sunToggled) {
    button.classList.add('sundial-on');
    sunIndicator.textContent = "🌒";
  } else {
    button.classList.remove('sundial-on');
    sunIndicator.textContent = "🌞";
  }
}
</script>

<style>
.sundial-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.sundial-button {
  border: none;
  padding: 20px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 223, 0, 0.9) 0%, rgba(255, 165, 0, 0.8) 100%);
  color: rgba(255, 255, 255, 0.8);
  font-size: 24px;
  cursor: pointer;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 1s ease-in-out;
}
.sundial-on {
  transform: rotate(180deg);
  background: radial-gradient(circle, rgba(30, 30, 36, 0.9) 0%, rgba(85, 85, 112, 0.8) 100%);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}
.sun-indicator {
  transition: color 0.5s;
}
</style>
      
Thank you for reading this article.

Comments

No comments yet. Be the first to comment!

More toggle-buttons

Similar

See also