通过css属性实现如图所示按钮
要点:通过checkbox选中与否的状态与兄弟选择器实现相关功能
1.设置开关大小并设置定位方式为relative
.swift-btn {
position: relative;
display: inline-block;
width: 50px;
height: 25px;
}
2.设置checkbox属性,并设置为不可见状态
.swift-btn input[type='checkbox'] {
position: absolute;
width: 50px;
height: 25px;
opacity: 0;
z-index: 9;
outline: none;
}
3.设置按钮样式,通过伪类元素实现,并添加过渡动画
.swift-btn > label{
position: absolute;
display: inline-block;
width: 50px;
height: 25px;
border-radius: 25px;
border: 1px solid #ddd;
transition: all 0.3s ease;
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
}
.swift-btn > label:before {
content: '';
position: absolute;
display: inline-block;
left: 40px;
width: 25px;
height: 25px;
border-radius: 20px;
line-height: 25px;
text-indent: -1px;
}
.swift-btn > label:after {
content: '';
position: absolute;
display: inline-block;
width: 24px;
height: 24px;
left: 0;
top: 1px;
border-radius: 1px;
box-shadow:2px 0px 1px #ddd;
border-radius: 15px;
background-color: #fff;
transition: left 0.3s ease;
-webkit-transition: left 0.3s ease;
-moz-transition: left 0.3s ease;
}
4.设置input选中后的样式
.swift-btn input[type='checkbox']:checked + label{
border: 1px solid #000;
background-color: #000;
}
.swift-btn input[type='checkbox']:checked + label::before {
display: inline-block;
color: #fff;
text-indent: -25px;
}
.swift-btn input[type='checkbox']:checked + label::after {
left: 26px;
top: 1px;
width: 24px;
height: 24px;
border: 0;
box-shadow: none;
box-sizing: content-box;
}
完整代码示例
<!DOCTYPE html>
<html>
<meta charset='utf-8'>
<style>
.swift-btn {
position: relative;
display: inline-block;
width: 50px;
height: 25px;
}
.swift-btn input[type='checkbox'] {
position: absolute;
width: 50px;
height: 25px;
opacity: 0;
z-index: 9;
outline: none;
}
.swift-btn > label{
position: absolute;
display: inline-block;
width: 50px;
height: 25px;
border-radius: 25px;
border: 1px solid #ddd;
transition: all 0.3s ease;
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
}
.swift-btn > label:before {
content: '';
position: absolute;
display: inline-block;
left: 40px;
width: 25px;
height: 25px;
border-radius: 20px;
line-height: 25px;
text-indent: -1px;
}
.swift-btn > label:after {
content: '';
position: absolute;
display: inline-block;
width: 24px;
height: 24px;
left: 0;
top: 1px;
border-radius: 1px;
box-shadow:2px 0px 1px #ddd;
border-radius: 15px;
background-color: #fff;
transition: left 0.3s ease;
-webkit-transition: left 0.3s ease;
-moz-transition: left 0.3s ease;
}
.swift-btn input[type='checkbox']:checked + label{
border: 1px solid #000;
background-color: #000;
}
.swift-btn input[type='checkbox']:checked + label::before {
display: inline-block;
color: #fff;
text-indent: -25px;
}
.swift-btn input[type='checkbox']:checked + label::after {
left: 26px;
top: 1px;
width: 24px;
height: 24px;
border: 0;
box-shadow: none;
box-sizing: content-box;
}
</style>
<body>
<span class="add-item-status swift-btn">
<input type="checkbox" name="" id='checkbox' />
<label class="" for='checkbox'></label>
</span>
</body>
</html>
判断是否选中可通过判断chekbox选中状态来判断,如通过jQuery判断方法:
var status = $('#checkbox').is(':checked');
热门评论
最近正在找关于这方面的文章,楼主写的简单明了,很受用
最近正在找关于这方面的文章,楼主写的简单明了,很受用
粗略步骤:
1) 设置定位让三个标签堆叠到一起
span =>包裹层,相对定位
span>input =>复选框(checkbox),绝对定位,
span>input+label =>for属性绑定复选框,绝对定位
2)调节相关标签样式
圆角、背景色、边框、伪元素添加按钮
1.使用::after/::before向label中追加一个圆形按钮
2.按钮设置绝对定位,设置初始值,left:0;
3.当复选框被选中时,通过改变按钮left值,配合transition(过渡)实现按钮左右滑动效果
也可以通过改变margin-left,实现效果
刚开始看有点懵逼,跟着做了会,稍微总结一下思路:
1.复选框隐藏;
2.label标签与复选框绑定;
3.伪元素向label追加圆形按钮;
4.根据复选框选中与否,改变圆形按钮位置,使用transition过渡;
在这里感觉作者的手记,学习了。欢迎大家交流,共同进步。