自定义常用input表单元素的第三篇,自定义一个Switch开关,表面上看是和input没关系,其实这里采用的是checkbox的checked值的切换。同样,采用css伪类和“+”css选择器为思路,下面是预览图:

下面先放HTML代码,看下DOM结构:

<input type="checkbox" id="my_switch" class="my_switch">
<label for="my_switch"></label>

DOM结构没什么特别的,就是一个常用的checkbox复选框的结构,唯一不同的是label标签没有内容。下面再看下CSS代码:

.my_switch {
display: none;
} .my_switch+label {
display: inline-block;
box-sizing: border-box;
height: 22px;
min-width: 44px;
line-height: 20px;
vertical-align: middle;
border-radius: 100px;
border: 1px solid transparent;
background-color: rgba(0, 0, 0, 0.25);
cursor: pointer;
-webkit-transition: all 0.36s;
transition: all 0.36s;
position: relative;
} .my_switch:checked+label {
background-color: #1890ff;
} .my_switch+label::before {
content: "";
display: block;
width: 18px;
height: 18px;
position: absolute;
left: 1px;
top: 1px;
border-radius: 18px;
background-color: #fff;
cursor: pointer;
transition: all 0.36s cubic-bezier(0.78, 0.14, 0.15, 0.86);
-webkit-transition: all 0.36s cubic-bezier(0.78, 0.14, 0.15, 0.86);
-webkit-box-shadow: 0 2px 4px 0 rgba(0, 35, 11, 0.2);
box-shadow: 0 2px 4px 0 rgba(0, 35, 11, 0.2);
} .my_switch:checked+label::before {
left: 23px;
transition: all 0.36s cubic-bezier(0.78, 0.14, 0.15, 0.86);
-webkit-transition: all 0.36s cubic-bezier(0.78, 0.14, 0.15, 0.86);
} .my_switch+label::after {
content: "关";
position: absolute;
top: 1px;
left: 24px;
font-size: 12px;
color: #fff
} .my_switch:checked+label::after {
content: "开";
left: 5px;
}

主要思路:switch开关有三部分组成,第一部分就是整个开关背景(椭圆部分)这部分可以用label定义样式。第二部分为左右切换的小按钮,可以用label伪类表示。第三部分为显示的开关文字,同样也可以用伪类表示。最终根据checkbox复选框的checked值就可以切换开或关了。赶快去试试吧。

最新文章

  1. openfire 初始密码
  2. python virtualenv 安装运行saltstack
  3. 100197C
  4. C#利用Attribute实现简易AOP介绍 (转载)
  5. 论javascript模块化的优缺
  6. OCA读书笔记(11) - 实现Oracle数据库审计
  7. Cocos2d-x项目总结中的一些遇到的问题
  8. UIScrollView 和 UIPageControl
  9. 大话python模块与包
  10. Python3基础知识
  11. 小老虎的微信公众号:iITObserve
  12. openfalcon架构及相关服务配置详解
  13. 第一个Struts2实例之hello world!
  14. Anroid 手机助手 详细解析 概述(二)
  15. ubuntu mongodb backup/restore (备份和恢复)
  16. EF 调用oracle 存储过程
  17. 【图论】POJ-3723 最大生成树
  18. 什么是Emit,什么是反射,二者区别到底是什么?(转)
  19. linux grep打印匹配的上下几行
  20. Linux中解压缩命令gzip和unzip的一点说明

热门文章

  1. appium的基本环境配置
  2. Educational Codeforces Round 95(A-C题解)
  3. C#开发PACS医学影像处理系统(十三):绘图处理之病灶测量
  4. 一篇文章说清楚TDengine的FQDN
  5. SpringBoot-02-原理初探之主启动类
  6. hystrix(4) 异常降级
  7. MVC设计模式-笔记1
  8. 【转】postgreSQL​之autovacuum性能问题分析(二)
  9. GitLab集成kubernetes
  10. 分享一些好用的 Chrome 插件!