<!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{ 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: ""; 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>