How To Create 3d Toggle Switch

In this blog,

We are going how to create custom 3d toggle  switch button.

Step-1: Create Html Code in the index.html file.

<body>
    <input class="switch-btn" type="checkbox" name="toggle" value="on">
</body>

Step-2: Add CSS Code in the <style>…</style> on index.html file.

 * {
  border: 0;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
body, input {
  font: 80px/1.5 sans-serif;
}

body {
  background-position: 50% calc(50% + 0.2em);
  display: grid;
  place-items: center;
  height: 100vh;
}
input[type=checkbox] {
  --off: #c7cad1;
  --mid: #354878;
  --on: #ffffff;
  --transDur: 0.5s;
  --timing: cubic-bezier(0.6,0,0.4,1);
  animation: bgOff var(--transDur) var(--timing);
  background-color: var(--off);
  border-radius: 0.67em / 0.5em;
  box-shadow:
    0 0.05em 0.1em #00000007 inset,
    0 -0.25em 0.25em #0001 inset,
    0 -0.5em 0 #0001 inset,
    0 0.1em 0.1em #0001;
  cursor: pointer;
  position: relative;
  width: 2.25em;
  height: 1.5em;
  -webkit-appearance: none;
  appearance: none;
  -webkit-tap-highlight-color: transparent;
}
input[type=checkbox]:before {
  animation: handleOff var(--transDur) var(--timing);
  background-attachment: fixed;
  background-position: 50% calc(50% - 0.1875em);
  border-radius: 0.5em / 0.375em;
  box-shadow:
    0 0.175em 0.175em 0 #0001 inset,
    0 0.375em 0 #0002 inset,
    0 0.375em 0 var(--off) inset,
    0 0.475em 0.1em #0001 inset;
  content: "";
  display: block;
  position: absolute;
  top: 0.125em;
  left: 0.125em;
  width: 1em;
  height: 0.75em;
}
input[type=checkbox]:checked {
  animation: bgOn var(--transDur) var(--timing) forwards;
}
input[type=checkbox]:checked:before {
  animation: handleOn var(--transDur) var(--timing) forwards;
}
input[type=checkbox]:focus {
  outline: none;
}
input[type=checkbox].switch-btn, input[type=checkbox].switch-btn:before {
  animation: none;
}
/* Dark mode */
@media (prefers-color-scheme: dark) {
  body, input[type=checkbox]:before {
    background-image:
      linear-gradient(90deg,#3a3d46 2px,#3a3d4600 2px),
      linear-gradient(#3a3d46 2px,#2e3138 2px);
  }
  input[type=checkbox] {
    --off: #5c6270;
    --mid: #354878;
  }
}
/* Animations */
@keyframes bgOff {
  from {
    background-color: var(--on);
  }
  50% {
    background-color: var(--mid);
  }
  to {
    background-color: var(--off);
  }
}
@keyframes bgOn {
  from {
    background-color: var(--off);
  }
  50% {
    background-color: var(--mid);
  }
  to {
    background-color: var(--on);
  }
}
@keyframes handleOff {
  from {
    box-shadow:
      0 0.175em 0.175em 0 #0001 inset,
      0 0.375em 0 #0002 inset,
      0 0.375em 0 var(--on) inset,
      0 0.475em 0.1em #0001 inset;
    left: 1.125em;
    width: 1em;
  }
  50% {
    box-shadow:
      0 0.175em 0.175em 0 #0001 inset,
      0 0.375em 0 #0002 inset,
      0 0.375em 0 var(--mid) inset,
      0 0.475em 0.1em #0001 inset;
    left: 0.125em;
    width: 2em;
  }
  to {
    box-shadow:
      0 0.175em 0.175em 0 #0001 inset,
      0 0.375em 0 #0002 inset,
      0 0.375em 0 var(--off) inset,
      0 0.475em 0.1em #0001 inset;
    left: 0.125em;
    width: 1em;
  }
}
@keyframes handleOn {
  from {
    box-shadow:
      0 0.175em 0.175em 0 #0001 inset,
      0 0.375em 0 #0002 inset,
      0 0.375em 0 var(--off) inset,
      0 0.475em 0.1em #0001 inset;
    left: 0.125em;
    width: 1em;
  }
  50% {
    box-shadow:
      0 0.175em 0.175em 0 #0001 inset,
      0 0.375em 0 #0002 inset,
      0 0.375em 0 var(--mid) inset,
      0 0.475em 0.1em #0001 inset;
    left: 0.125em;
    width: 2em;
  }
  to {
    box-shadow:
      0 0.175em 0.175em 0 #0001 inset,
      0 0.375em 0 #0002 inset,
      0 0.375em 0 var(--on) inset,
      0 0.475em 0.1em #0001 inset;
    left: 1.125em;
    width: 1em;
  }
}

Step-3: Now I added code in my <script>…</script> tag on index.html file.

<script>
document.addEventListener("click",e => {
  let tar = e.target;
  if (tar.name == "toggle")
    tar.removeAttribute("class");
});
</script>

Review the below video.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories