Back

Unlocking the Power of a Magnetic Nano Toggle Switch

Dive into the creation of an innovative toggle switch inspired by the whimsical nature of magnetic slime, offering a tactile user experience.

Introduction

Today, we're crafting a 'Magnetic Nano Toggle Switch', a captivating take on the conventional toggle button, inspired by the unique and malleable properties of magnetic slime.

HTML Structure

We'll start with a simple HTML structure that includes a button element. This button serves as the interactive magnetic toggle switch.
        <div class="magnetic-nano-container">
          <button aria-label="Magnetic toggle switch" onclick="toggleNano()" class="magnetic-toggle-button">
            <span class="toggle-label">OFF</span>
          </button>
        </div>
      

CSS Styling

Next, we apply styles to give our toggle button a magnetic appearance. The button changes color and shadow to simulate the effect of magnetization.
        .magnetic-nano-container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .magnetic-toggle-button {
            border: 2px solid #ccc;
            padding: 20px;
            border-radius: 25px;
            color: white;
            font-size: 20px;
            font-weight: bold;
            cursor: pointer;
            background: rgba(192, 192, 192, 0.7);
            transition: background 0.3s, box-shadow 0.3s;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        .magnetized {
            box-shadow: 0 0 15px rgba(32, 64, 96, 0.8), inset 0 0 10px rgba(32, 64, 96, 0.7);
        }
      

JavaScript Logic

Our JavaScript manages the toggle state, switching the button's background color and shadow effect when clicked.
        let isToggled = false;
        function toggleNano() {
            const button = document.querySelector('.magnetic-toggle-button');
            const label = document.querySelector('.toggle-label');
            isToggled = !isToggled;
            if (isToggled) {
                button.style.background = "rgba(32, 64, 96, 0.7)";
                button.classList.add('magnetized');
                label.textContent = "ON";
            } else {
                button.style.background = "rgba(192, 192, 192, 0.7)";
                button.classList.remove('magnetized');
                label.textContent = "OFF";
            }
        }
      

SEO and Accessibility Considerations

We maintain accessibility by including descriptive aria-labels and ensuring all interactive elements have clear focus styles. Semantic elements and headings improve SEO.

Modern Visual Trends Integration

This toggle switch employs the visual trend of tactile motion through subtle CSS animations, providing a smooth and realistic interaction experience. Discover and experiment with this creative approach to toggle buttons, pushing the boundaries of interaction and aesthetics in UI design.

Whole code

<div class="magnetic-nano-container">
  <button aria-label="Magnetic toggle switch" onclick="toggleNano()" class="magnetic-toggle-button">
    <span class="toggle-label">OFF</span>
  </button>
</div>


<script>
let isToggled = false;
function toggleNano() {
  const button = document.querySelector('.magnetic-toggle-button');
  const label = document.querySelector('.toggle-label');
  isToggled = !isToggled;
  if (isToggled) {
    button.style.background = "rgba(32, 64, 96, 0.7)";
    button.classList.add('magnetized');
    label.textContent = "ON";
  } else {
    button.style.background = "rgba(192, 192, 192, 0.7)";
    button.classList.remove('magnetized');
    label.textContent = "OFF";
  }
}
</script>

<style>
.magnetic-nano-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.magnetic-toggle-button {
  border: 2px solid #ccc;
  padding: 20px;
  border-radius: 25px;
  color: white;
  font-size: 20px;
  font-weight: bold;
  cursor: pointer;
  background: rgba(192, 192, 192, 0.7);
  transition: background 0.3s, box-shadow 0.3s;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.magnetized {
  box-shadow: 0 0 15px rgba(32, 64, 96, 0.8), inset 0 0 10px rgba(32, 64, 96, 0.7);
}

</style>
      
Thank you for reading this article.

Comments

No comments yet. Be the first to comment!

More toggle-buttons

Similar

See also