Toggle Button Animation in HTML CSS & JavaScript

HTML
<div class="toggle"></div>
CSS
*{
    margin:0;
    padding:0;
    box-sizing:border-box;
}
body{
    height:100vh;
    display:flex;
    align-items:center;
    justify-content:center;
    background:#7d2ae8;
}
.toggle{
    position:relative;
    height:12px;
    width:125px;
    cursor:pointer;
    border-radius:25px;
    background-color:#fff;
    box-shadow:0 5px 10px rgba(0,0,0,0.1);
}
.toggle::before{
    content:"";
    position:absolute;
    left:0;
    top:50%;
    transform:translateY(-50%);
    height:50px;
    width:50px;
    border-radius:50%;
    background-color:#7d2ae8;
    border:10px solid #fff;
    box-shadow:0 5px 10px rgba(0,0,0,0.1);
    transition:all 0.5s cubic-bezier(0.68,-0.55,0.265,1.55);
}
.toggle.active::before{
    left:calc(100% - 70px);
    background-color:#fff;
    border-color:#7d2ae8;
}
JS
const toggleBtn = document.querySelector(".toggle");

toggleBtn.addEventListener("click",() => toggleBtn.classList.toggle("active"));

/* All the credits: https://www.youtube.com/watch?v=BqjSWo2Zy7s */

Autor: Leonard

Publicado: 24/12/2022

Compartir Elemento Web

Donar a Compositu
Otras formas de ayudar
  • Compártelo en tus redes sociales.
  • Recomienda los elementos.
  • Regístrate Aquí
  • Deja tu comentario agradeciendo el aporte.

Descarga el código completo del Elemento Web y archivos necesarios (imágenes, librerias, plugins, frameword, etc)

Deja tu comentario