原生js实现开关功能

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .kg{
            width: 45px;
            height: 20px;
            background-color: #dcdfe6;
            border-radius: 10px;
            border: 2px solid #dcdfe6;
        }
        .kg::after{
            transition: left 0.5s;
            display: block;
            left: 1px;
            top:2px;
            position: absolute;
            content: "";
            width: 20px;
            height: 20px;
            border-radius: 10px;
            background-color: #fff;
        }
        .hh{
            border-color: #409eff;
            background-color: #409eff;
        }
        .hh::after{
            left: 28px;
        }
    </style>
</head>
<body>
    <div class="kg">
    </div>
<script>
    var kg=document.querySelector(".kg");
    kg.onclick=function () {
        this.classList.toggle("hh")
    }
</script>
</body>
</html>

  

猜你喜欢

转载自www.cnblogs.com/daifuchao/p/11794840.html