使用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>

最新文章

  1. zepto区别于jquery获取select表单选中的值
  2. Linux下如何不停止服务,清空nohup.out文件
  3. hibernate的二级缓存
  4. connect调用超时的实现方式
  5. n阶乘 尾数0的个数
  6. 9款精致HTML5/jQuery日历时钟控件源码下载(源码请见百度云) 链接:http://pan.baidu.com/s/1geIXe75 密码:7m4a
  7. shiro实现APP、web统一登录认证和权限管理
  8. 详解Win2003 IIS6.0 301重定向带参数的问题(转摘)
  9. 如何使用ZBLibrary-Android快速开发框架
  10. hdu 验证角谷猜想 1279
  11. oracle数据库管理--用户管理
  12. Linux scp命令
  13. markdown基础
  14. python实战===使用随机的163账号发送邮件
  15. iOS开发之UIGestureRecognizer
  16. Java入门(五):控制流程
  17. Github 开源项目(二)gorun (go语言工具)
  18. OS面试题(转载)
  19. Linux/Python学习路线
  20. asp.net集合类

热门文章

  1. IDEA中Git的应用场景
  2. 微信小程序 新手入门教程
  3. leetcode 684. Redundant Connection
  4. sublime text2 相关插件及其应用
  5. ES6 对象的解构赋值
  6. java中Math常用方法
  7. 百度地图API应用之获取用户的具体位置
  8. OpenCV——PS滤镜算法之 Ellipsoid (凹陷)
  9. POJ1113:Wall (凸包:求最小的多边形,到所有点的距离大于大于L)
  10. iOS中NSNotification、delegate、KVO三者之间的区别与联系?