JSFiddle - React, Tailwind, and code Playground

by Kocsten

HTML

<h1 style="text-align: center; margin-top: 20px; margin-bottom: 50px; font-size: 28px;">CSS - Переключатель с использованием JavaScript</h1>

<div style="text-align: center;">

    <div class="switch-btn"></div>

    <br><br><br>

    <div class="switch-btn switch-on"></div>

</div>

CSS

.switch-btn {
    display: inline-block;
    width: 62px; /* ширина переключателя */
    height: 24px; /* высота переключателя */
    border-radius: 12px; /* радиус скругления */
    background: #bfbfbf; /* цвет фона */
    z-index: 0;
    margin: 0;
    padding: 0;
    border: none;
    cursor: pointer;
    position: relative;
    transition-duration: 300ms; /* анимация */
}
.switch-btn::after {
    content: "";
    height: 36px; /* высота кнопки */
    width: 36px; /* ширина кнопки */
    border-radius: 18px; /* радиус кнопки */
    background: #fff; /* цвет кнопки */
    top: -6px; /* положение кнопки по вертикали относительно основы */
    left: -6px; /* положение кнопки по горизонтали относительно основы */
    transition-duration: 300ms; /* анимация */
    box-shadow: 0 0 10px 0 #999999; /* тень */
    position: absolute;
    z-index: 1;
}
.switch-on {
    background: #fff;
    box-shadow: inset 0 0 10px 0 #999999; /* тень */
}
.switch-on::after {
    left: 30px;
    background: #118c4e;
}

JavaScript

$('.switch-btn').click(function(){
        $(this).toggleClass('switch-on');
    });