css3实现的简单开关按钮代码实例:

css3实现的简单开关按钮代码实例:
本章节分享一段代码实例,它使用css3实现了开关按钮效果。
代码和网上实用的相比相对粗陋一点,但是完全能够演示它实现的原理。
需要的朋友可以做一下参考,代码实例如下:
[HTML]  纯文本查看  复制代码 运行代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
< html >
< head >
< meta charset = " utf-8" >
< meta name = "author" content = "http://www.softwhy.com/" />
< title >蚂蚁部落</ title >
< style type = "text/css" >
input[type="checkbox"]{
   display:none;
}
label{
   box-shadow:#ccc 0px 0px 0px 1px;
   width:40px;
   height:20px;
   display:inline-block;
   border-radius:20px;
   position:relative;
   background-color:#bdbdbd;
   overflow:hidden;
}
label:before{
   content:'';
   position:absolute;
         
   left:0px;
   width:20px;
   height:20px;
   display:inline-block;
   border-radius:20px;
   background-color:#fff;
   z-index:20;
   -webkit-transition:all 0.5s;
   transition:all 0.5s;
}
 
input:checked + label:before{
   left:20px;   
}
input:checked + label{
   background-color:#51ccee;
}
</ style >
</ head >
< body >
< input type = "checkbox" name = "sex" id = "male" />
< label for = "male" ></ label >
</ body >
</ html >

上面的代码实现了我们的要求,下面介绍一下它的实现过程。
一.代码注释:
上面的功能是开和关是由复选框的选中和取消选中来实现的。
但是我们看到的效果并不是复选框,复选框处于隐藏状态,由以下代码实现:
[CSS]  纯文本查看  复制代码
1
2
3
input[type= "checkbox" ]{
   display : none ;
}

之所以点击的时候能够实现切换是由于使用<label>标签和checkbox复选框关联起来的原因。
[CSS]  纯文本查看  复制代码
01
02
03
04
05
06
07
08
09
10
label{
   box-shadow: #ccc 0px 0px 0px 1px ;
   width : 40px ;
   height : 20px ;
   display :inline- block ;
   border-radius: 20px ;
   position : relative ;
   background-color : #bdbdbd ;
   overflow : hidden ;
}

上面的代码规定了扁平开关活动区域,截图如下:
 
就是红色的框区域。
[CSS]  纯文本查看  复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
label:before{
   content : '' ;
   position : absolute ;
         
   left : 0px ;
   width : 20px ;
   height : 20px ;
   display :inline- block ;
   border-radius: 20px ;
   background-color : #fff ;
   z-index : 20 ;
   -webkit-transition: all 0.5 s;
   transition: all 0.5 s;
}

上面的代码规定了圆形的开关按钮。
是通过:before伪元素选择器添加的,它是绝对定位,这样可以通过调整它的left属性值实现左右移动效果。
又通过transition实现了左右移动动画效果。
[CSS]  纯文本查看  复制代码
1
2
3
input:checked + label:before{
   left : 20px ;
}

当复选框选中的时候,就将伪元素选择器添加的元素left值设置为20px,也就是移到右端。
[CSS]  纯文本查看  复制代码
1
2
3
input:checked + label{
   background-color : #51ccee ;
}

上面的设置label元素的背景颜色为#51ccee。
二.相关阅读:
(1).[type="checkbox"]可以参阅 CSS E[att="val"]属性选择符 一章节。
(2).box-shadow可以参阅
CSS3 box-shadow 一章节。
(3).border-radius可以参阅
CSS3 border-radius圆角 一章节。
(4).:before可以参阅
CSS E:before/E::before伪对象选择符 一章节。
(5).transition可以参阅
CSS3 transition 一章节。
(6).:checked可以参阅
CSS E:checked伪类选择符 一章节。

(7).+选择器可以参阅CSS (E+F)相邻选择符一章节。

转自:http://www.softwhy.com/forum.php?mod=viewthread&tid=18836
http://www.softwhy.com/forum.php?mod=viewthread&tid=18836
(出处: 蚂蚁部落)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用CSS3中的伪类和过渡属性来实现滑动按钮的效果。以下是一个简单实现示例: HTML代码: ```html <label class="switch"> <input type="checkbox"> <span class="slider"></span> </label> ``` CSS代码: ```css .switch { position: relative; display: inline-block; width: 60px; height: 34px; } .switch input[type="checkbox"] { display: none; } .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; } .slider:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; transition: .4s; } input:checked + .slider { background-color: #2196F3; } input:focus + .slider { box-shadow: 0 0 1px #2196F3; } input:checked + .slider:before { transform: translateX(26px); } .slider.round { border-radius: 34px; } .slider.round:before { border-radius: 50%; } ``` 解释: - `.switch`类设置了开关按钮的基本样式,包括宽高、定位等。 - `input[type="checkbox"]`选择器隐藏了原生的复选框。 - `.slider`类设置了按钮的背景色、过渡效果等。 - `.slider:before`伪类设置了按钮内部的小圆点,以及其过渡效果。 - `input:checked + .slider`选择器设置了按钮选中时的背景色。 - `input:focus + .slider`选择器设置了按钮获取焦点时的阴影效果。 - `input:checked + .slider:before`选择器设置了按钮选中时,内部小圆点的位移效果。 - `.slider.round`和`.slider.round:before`类用于设置圆形按钮的样式,可根据需要选择使用。 这样就可以实现一个简单的滑动按钮效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值