Back

Crafting a Futuristic Time Machine Toggle Button

Dive into the creation of a toggle button resembling a miniaturized time machine, bringing grand adventure with each activation through time-themed visuals and sound.

Component Explanation

Crafting a Futuristic Time Machine Toggle Button

Welcome to a delightful journey of creating a time-machine-inspired toggle button, a UI component that does more than just function; it evokes the excitement of adventure. In this tutorial, every step is carefully designed with accessibility and engaging visuals in mind. By creatively leveraging CSS and JavaScript, we’ll construct a button that not only toggles states visually but also invites the user into a world of time travel.

HTML Setup

In our HTML structure, we utilize semantic elements to ensure clarity and accessibility. This helps screen readers and other assistive technologies process our components effectively. We begin by defining a container element that will hold our button. This container centers our button using CSS flexbox, making it aesthetically balanced regardless of screen size.

      <div class="time-machine-container">
        <button onclick="toggleTimeTravel()" class="time-machine-button" aria-label="Activate Time Machine">
          <span class="time-status">ENGAGE</span>
        </button>
      </div>
      

The button element includes an aria-label, crucial for accessibility purposes, providing context for users relying on screen readers. An engaging text is dynamically displayed within a span, changing with interactions.

JavaScript Dynamics

The JavaScript portion brings life to our button. It handles the state changes that switch between 'ENGAGE' and 'TRAVELING'. The script modifies CSS properties directly, providing visible feedback that enhances user interaction.

      let engaged = false;
      function toggleTimeTravel() {
          let button = document.querySelector('.time-machine-button');
          let statusText = document.querySelector('.time-status');
          engaged = !engaged;
          if (engaged) {
              button.style.background = "radial-gradient(circle, rgba(255,223,186,1) 0%, rgba(114,91,238,1) 100%)";
              statusText.textContent = "TRAVELING";
              button.classList.add('engaged');
          } else {
              button.style.background = "radial-gradient(circle, rgba(163,195,255,0.9) 0%, rgba(58,123,213,0.9) 100%)";
              statusText.textContent = "ENGAGE";
              button.classList.remove('engaged');
          }
      }
      

This script handles the state transitions by toggling a boolean variable and altering the styling accordingly. The 'engaged' class is added or removed to produce visual effects, creating an illusion of a button lighting up as if activating a time machine.

Enchanting with CSS

CSS is where the magic happens. The visual transition when toggling is smooth and enchanting, achieved through gradients and shadow effects inspired by futuristic and sci-fi aesthetics.

      .time-machine-container {
          display: flex;
          justify-content: center;
          align-items: center;
          height: 100vh;
      }
      

The .time-machine-container class uses flexbox to center our button, ensuring it attracts attention. By applying full viewport height, we maintain a focus on the button itself, which is key for user engagement.

      .time-machine-button {
          border: none;
          padding: 20px 40px;
          border-radius: 12px;
          color: #ffffff;
          font-size: 22px;
          font-weight: bold;
          transition: background 0.5s, box-shadow 0.3s;
          cursor: pointer;
          background: radial-gradient(circle, rgba(163,195,255,0.9) 0%, rgba(58,123,213,0.9) 100%);
      }
      

The button is styled with a gentle gradient that hints at the cosmic, a visual metaphor for the concept of time travel. Its radius gives a modern, inviting appearance, and transitions applied to background and shadow effects animate our button beautifully when toggled.

      .engaged {
          box-shadow: 0 0 20px rgba(114,91,238,0.7), 0 0 40px rgba(114,91,238,0.7);
          transform: rotate(360deg);
      }
      

The .engaged class introduces a rotational effect and glow, elements that mimic the energetic activation of a sci-fi device. Adjustments ensure the animation remains sleek without hindering performance or accessibility.

In conclusion, this time machine toggle button encapsulates playful innovation. It is a testament to how we can blend entertainment with function in UI design, making even the simplest interaction a delight.

Whole code

<div class="time-machine-container">
  <button onclick="toggleTimeTravel()" class="time-machine-button" aria-label="Activate Time Machine">
    <span class="time-status">ENGAGE</span>
  </button>
</div>


<script>
let engaged = false;
function toggleTimeTravel() {
  let button = document.querySelector('.time-machine-button');
  let statusText = document.querySelector('.time-status');
  engaged = !engaged;
  if (engaged) {
    button.style.background = "radial-gradient(circle, rgba(255,223,186,1) 0%, rgba(114,91,238,1) 100%)";
    statusText.textContent = "TRAVELING";
    button.classList.add('engaged');
  } else {
    button.style.background = "radial-gradient(circle, rgba(163,195,255,0.9) 0%, rgba(58,123,213,0.9) 100%)";
    statusText.textContent = "ENGAGE";
    button.classList.remove('engaged');
  }
}
</script>

<style>
.time-machine-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.time-machine-button {
  border: none;
  padding: 20px 40px;
  border-radius: 12px;
  color: #ffffff;
  font-size: 22px;
  font-weight: bold;
  transition: background 0.5s, box-shadow 0.3s;
  cursor: pointer;
  background: radial-gradient(circle, rgba(163,195,255,0.9) 0%, rgba(58,123,213,0.9) 100%);
}
.engaged {
  box-shadow: 0 0 20px rgba(114,91,238,0.7), 0 0 40px rgba(114,91,238,0.7);
  transform: rotate(360deg);
}

</style>
      
Thank you for reading this article.

Comments

No comments yet. Be the first to comment!

More toggle-buttons

Similar

See also