原理是用伪类来实现 难点在背景颜色的变化 因为没有部分控制父元素的背景颜色变化 所以用before这个伪类来实现背景变化效果效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
body {
display: flex;
align-items: center;
justify-content: center;
/* background: #fff; */
min-height: 100vh;
}
label {
position: relative;
transform: scale(2);
/* background-color: #ddd; */
width: 100px;
height: 50px;
border: 1px solid #ddd;
background-color: #ddd;
border-radius: 25px;
overflow: hidden;
}
label input {
visibility: hidden;
}
label input::after {
visibility: visible;
content: '';
position: absolute;
left: 5px;
top: 5px;
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #fff;
transition: all 0.2;
}
label input::before {
visibility: visible;
content: '';
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
transition: all 0.2;
}
label input:checked::after {
transform: translateX(50px);
}
label input:checked::before {
background-color: rgb(10, 241, 10);
}
</style>
</head>
<body>
<label>
<input type="checkbox" class="checkBox" />
</label>
</body>
</html>