原理:
月牙形状其实就是圆形的box 加上box-shadow 来视觉上形成月牙状。刚好配合 input checkbox 的 checked属性 可以做一个css值切换。
html
<label class="switch">
<input type="checkbox" checked />
<span class="slider"></span>
</label>
css
.switch {
font-size: 17px;
position: relative;
display: inline-block;
width: 3.5em;
height: 1.5em;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color:#522ba7;
transition: 0.5s;
border-radius: 30px;
}
.slider:before {
position: absolute;
content: "";
height: 1.1em;
width: 1.1em;
border-radius: 50%;
left: 11%;
bottom: 15%;
box-shadow: inset 8px -4px 0px 0px #fff000;
background: var(--background);
transition: 0.5s;
}
input:checked + .slider {
background-color:blue
}
input:checked + .slider:before {
transform: translateX(150%);
box-shadow: inset 15px -4px 0px 15px #fff000;
}
image.png
网友评论