css页
/* 必须input为CheckBox,class添加switch才能实现以下效果 */
input[type="checkbox"].wl-switch{
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
position: relative;
width: 2.8rem;
height: 1.4rem;
border:1px solid #ccc;
background-color: #ccc;
border-radius: 0.7rem;
outline: none;
}
input[type="checkbox"].wl-switch::after{
content: "";
display: inline-block;
width: 1.4rem;
height: 1.39rem;
border-radius: 50%;
background-color: #fff;
box-shadow: 0 0 2px #999;
transition: left 0.1s;
position: absolute;
top: -1px;
left:0;
}
input[type="checkbox"].wl-switch:checked{
background-color: #32ba58;
}
/*
1.默认为不被选中状态
input[type="checkbox"] 被选中时,伪元素显示下面样式,定位发生变化
*/
input[type="checkbox"].wl-switch:checked::after{
position: absolute;
left:1.4rem;
top: -1px;
}
/* 状态 */
input[type="checkbox"]:checked.wl-switch.wl-switch-info{
background-color: #409eff;
}
input[type="checkbox"]:checked.wl-switch.wl-switch-danger{
background-color: rgb(255, 73, 73);
}
input[type="checkbox"]:checked.wl-switch.wl-switch-warm{
background-color: hsl(51,93%,54%);
}
input[type="checkbox"]:checked.wl-switch.wl-switch-primary{
background-color: #409eff;
}
input[type="checkbox"].wl-switch.wl-switch-primary{
background-color: rgb(255, 73, 73);
}
html页
<body>
<input type="checkbox" class="wl-switch">
<input type="text" class="wl-switch">
<input type="checkbox" class="wl-switch wl-switch-info">
<input type="checkbox" class="wl-switch wl-switch-warm">
<input type="checkbox" class="wl-switch wl-switch-danger">
<input type="checkbox" class="wl-switch wl-switch-primary">
</body>
运行图