【HTML】
<input type="checkbox" name="" id="obj" class="chooseBtn" />
<label for="obj" class="choose-label"></label>
【CSS】
.chooseBtn {
display: none;/*input不可见,实际只用到了lable*/
}
.choose-label {
box-shadow: #ccc 0px 0px 0px 1px;
width: 60px;/*长条长度*/
height: 30px;/*长条高度*/
display: inline-block;
border-radius: 20px;
position: relative;
background-color: #bdbdbd;
overflow: hidden;
}
/*小圆点*/
.choose-label:before {
content: '';
position: absolute;
left: 0;
width: 30px;/*圆点半径*/
height: 30px;/*圆点半径*/
display: inline-block;
border-radius: 20px;
background-color: #fff;
z-index: 20;
-webkit-transition: all 0.5s;
transition: all 0.5s;
}
.chooseBtn:checked + label.choose-label:before {
left: 30px;/*圆点距离左边的距离,设置长条的一半较好*/
}
.chooseBtn:checked + label.choose-label {
background-color: #3CB371;/*绿色*/
}