使用css3 制作switch开关
使用css3来实现switch开关的效果:
html代码:
<!--switch开关-->
<div class="switch-btn">
<input type='checkbox' id='switchs' checked class='switch-checkbox'>
<label for='switchs' class='switch-left'></label>
<label for='switchs' class='switch-right'></label>
</div>
label标签for属性的作用:将for属性值与input 中id值相等的标签关联起来,当点击label标签时,如radio,checkbox会随之选中或者不选中
css代码:
.switch-btn{
position: relative;
}
.switch-btn .switch-checkbox{
display:none;
}
.switch-btn .switch-left{
display: block;
width: 1.5rem;
height: 1.5rem;
left: 1.5rem;
top: 0;
border-radius: 50%;
position: absolute;
transition: 0.4s;
background: #fff;
z-index: 1;
box-shadow: 1px 1px 2px #ccc;
}
.switch-btn .switch-checkbox:checked + .switch-left{
left:0px;
box-shadow:-1px 1px 2px #ccc;
}
.switch-btn .switch-right{
display: block;
width: 3rem;
top: 0;
height: 1.5rem;
border-radius: 1rem;
transition: 0.4s;
background: #999;
position: absolute;
}
.switch-btn .switch-checkbox:checked + .switch-left + .switch-right{
background: #04BE02;
transition:0.4s;
}
transform(变形,转换)http://www.w3school.com.cn/cssref/pr_transform.asp
transform的属性包括:rotate() / skew() / scale() / translate(,) ,分别还有x、y之分,比如:rotatex() 和 rotatey() ,translateX和translateY以此类推。
transition(过渡)http://www.w3school.com.cn/css3/css3_transition.asp
transition的属性值包括:transition-property(过渡属性名 all 必填),transition-duration(过渡周期时间 0.3s 必填),transition-timing-function(过渡方式 ease 必填),transition-delay(延迟时间 选填)
说明:.switch-checkbox:checked + .switch-left (.switch-checkbox 被选中的时候后面的兄弟元素 设置.switch-left样式)
http://www.w3school.com.cn/cssref/selector_gen_sibling.asp
<script>
//使用onchange事件,获取当前是否是checked
var checkSwitch= document.getElementById('switchs');
checkSwitch.onchange=function(event){
console.log(event.target.checked);
}
</script>
最新文章
- zepto区别于jquery获取select表单选中的值
- Linux下如何不停止服务,清空nohup.out文件
- hibernate的二级缓存
- connect调用超时的实现方式
- n阶乘 尾数0的个数
- 9款精致HTML5/jQuery日历时钟控件源码下载(源码请见百度云) 链接:http://pan.baidu.com/s/1geIXe75 密码:7m4a
- shiro实现APP、web统一登录认证和权限管理
- 详解Win2003 IIS6.0 301重定向带参数的问题(转摘)
- 如何使用ZBLibrary-Android快速开发框架
- hdu 验证角谷猜想 1279
- oracle数据库管理--用户管理
- Linux scp命令
- markdown基础
- python实战===使用随机的163账号发送邮件
- iOS开发之UIGestureRecognizer
- Java入门(五):控制流程
- Github 开源项目(二)gorun (go语言工具)
- OS面试题(转载)
- Linux/Python学习路线
- asp.net集合类