使用CSS3编写类似iOS中的复选框及带开关的按钮


Posted in HTML / CSS onApril 11, 2016

checkbox多选

最近写了一个适合移动端的checkbox,如图:
使用CSS3编写类似iOS中的复选框及带开关的按钮

ps:中间的勾勾是iconfont,iOS风格的。

具体的HTML:

XML/HTML Code复制内容到剪贴板
  1. <div class="mui-checkbox-con">  
  2.     <label>  
  3.         <input class="mui-checkbox" type="checkbox">默认未选中</label>  
  4. </div>  
  5. <div class="mui-checkbox-con">  
  6.     <label>  
  7.         <input class="mui-checkbox" type="checkbox" checked>默认选中</label>  
  8. </div>  
  9. <div class="mui-checkbox-con">  
  10.     <label>  
  11.         <input class="mui-checkbox checkbox-orange" type="checkbox" checked>橘黄色 checkbox-orange</label>  
  12. </div>  
  13. <div class="mui-checkbox-con">  
  14.     <label>  
  15.         <input class="mui-checkbox checkbox-green" type="checkbox" checked>绿色 checkbox-green</label>  
  16. </div>  
  17. <div class="mui-checkbox-con">  
  18.     <label>  
  19.         <input class="mui-checkbox" type="checkbox" disabled>禁用</label>  
  20. </div>  

CSS代码(SCSS导出的,排版有些奇怪):

CSS Code复制内容到剪贴板
  1. .mui-checkbox {   
  2.   -webkit-appearance: none;   
  3.   positionrelative;   
  4.   width25px;   
  5.   height25px;   
  6.   margin-right10px;   
  7.   background-color#FFFFFF;   
  8.   bordersolid 1px #d9d9d9;   
  9.   border-top-left-radius: 20px;   
  10.   border-top-rightright-radius: 20px;   
  11.   border-bottom-left-radius: 20px;   
  12.   border-bottom-rightright-radius: 20px;   
  13.   background-clippadding-box;   
  14.   displayinline-block; }   
  15.   .mui-checkbox:focus {   
  16.     outline: 0 none;   
  17.     outline-offset: -2px; }   
  18.   .mui-checkbox:checked {   
  19.     background-color#18b4ed;   
  20.     bordersolid 1px #FFFFFF; }   
  21.     .mui-checkbox:checked:before {   
  22.       displayinline-block;   
  23.       margin-top1px;   
  24.       margin-left2px;   
  25.       font-family: iconfont;   
  26.       content"\e667";   
  27.       color#FFFFFF;   
  28.       font-size18px; }   
  29.   .mui-checkbox:disabled {   
  30.     background-color#d9d9d9;   
  31.     bordersolid 1px #d9d9d9; }   
  32.     .mui-checkbox:disabled:before {   
  33.       displayinline-block;   
  34.       margin-top1px;   
  35.       margin-left2px;   
  36.       font-family: iconfont;   
  37.       content"\e667";   
  38.       color#FFFFFF;   
  39.       font-size18px; }   
  40.   .mui-checkbox.checkbox-green:checked {   
  41.     background-color#5cb85c; }   
  42.   .mui-checkbox.checkbox-orange:checked {   
  43.     background-color#f0ad4e; }   
  44.   .mui-checkbox.checkbox-s {   
  45.     width19px;   
  46.     height19px; }   
  47.     .mui-checkbox.checkbox-s:before {   
  48.       displayinline-block;   
  49.       margin-top1px;   
  50.       margin-left2px;   
  51.       font-family: iconfont;   
  52.       content"\e667";   
  53.       color#FFFFFF;   
  54.       font-size13px; }   
  55.     
  56. .mui-checkbox-anim {   
  57.   -webkit-transition: background-color ease 0.2s;   
  58.           transition: background-color ease 0.2s; }  

SCSS代码:

CSS Code复制内容到剪贴板
  1. @mixin checkedCon($fs:18px) {   
  2.   &:before {   
  3.     displayinline-block;   
  4.     margin-top1px;   
  5.     margin-left2px;   
  6.     font-family: iconfont;   
  7.     content"\e667";   
  8.     color#FFFFFF;   
  9.     font-size: $fs;   
  10.   }   
  11. }   
  12. $duration: .4s;   
  13. .mui-checkbox {   
  14.   -webkit-appearance: none;   
  15.   positionrelative;   
  16.   width25px;   
  17.   height25px;   
  18.   margin-right10px;   
  19.   background-color#FFFFFF;   
  20.   bordersolid 1px #d9d9d9;   
  21.   border-top-left-radius: 20px;   
  22.   border-top-rightright-radius: 20px;   
  23.   border-bottom-left-radius: 20px;   
  24.   border-bottom-rightright-radius: 20px;   
  25.   background-clippadding-box;   
  26.   displayinline-block;   
  27.   &:focus {   
  28.     outline: 0 none;   
  29.     outline-offset: -2px  
  30.   }   
  31.   &:checked {   
  32.     background-color#18b4ed;   
  33.     bordersolid 1px #FFFFFF;   
  34.     @include checkedCon();   
  35.   }   
  36.   &:disabled {   
  37.     background-color#d9d9d9;   
  38.     bordersolid 1px #d9d9d9;   
  39.     @include checkedCon();   
  40.   }   
  41.   &.checkbox-green:checked {   
  42.     background-color#5cb85c;   
  43.   }   
  44.   &.checkbox-orange:checked {   
  45.     background-color#f0ad4e;   
  46.   }   
  47.   &.checkbox-s {   
  48.     width19px;   
  49.     height19px;   
  50.     @include checkedCon(13px);   
  51.   }   
  52. }   
  53.     
  54. .mui-checkbox-anim{   
  55.   //border等其他元素不做过渡效果,增加视觉差,更有动画效果   
  56.   transition: background-color ease $duration/2;   
  57.     
  58. }  

 
带switch开关
本身我做这一个ui的目的是支持移动端的页面,而webkit上也正好支持单标记的input元素是使用伪类(:before或:after),所以我没做更多的支持和优化,我只是想尽量的保持html干净,所以没用其他元素做模拟。如果你要使用在桌面应用上,或支持其他浏览器,可以自己稍微修改一下,反正我是没测试过。

今天继续分享一个iOS风格的switch开关按钮,样子也非常常见,如图:
使用CSS3编写类似iOS中的复选框及带开关的按钮

主要是使用了<input  type="checkbox">来模拟实现,具体的HTML:

XML/HTML Code复制内容到剪贴板
  1. <label><input class="mui-switch" type="checkbox"> 默认未选中</label>  
  2. <label><input class="mui-switch" type="checkbox" checked> 默认选中</label>  
  3. <label><input class="mui-switch mui-switch-animbg" type="checkbox"> 默认未选中,简单的背景过渡效果,加mui-switch-animbg类即可</label>  
  4. <label><input class="mui-switch mui-switch-animbg" type="checkbox" checked> 默认选中</label>  
  5. <label><input class="mui-switch mui-switch-anim" type="checkbox"> 默认未选中,过渡效果,加 mui-switch-anim   
  6. 类即可</label>  
  7. <label><input class="mui-switch mui-switch-anim" type="checkbox" checked> 默认选中</label>  

在实际的使用中后来又增加了两个过渡效果,分别加 mui-switch-animbg和mui-switch-anim 类即可,具体效果查看下面的demo页面。

CSS代码(SCSS导出的,排版有些奇怪):

CSS Code复制内容到剪贴板
  1. .mui-switch {   
  2.   width52px;   
  3.   height31px;   
  4.   positionrelative;   
  5.   border1px solid #dfdfdf;   
  6.   background-color#fdfdfd;   
  7.   box-shadow: #dfdfdf 0 0 0 0 inset;   
  8.   border-radius: 20px;   
  9.   border-top-left-radius: 20px;   
  10.   border-top-rightright-radius: 20px;   
  11.   border-bottom-left-radius: 20px;   
  12.   border-bottom-rightright-radius: 20px;   
  13.   background-clipcontent-box;   
  14.   displayinline-block;   
  15.   -webkit-appearance: none;   
  16.   user-select: none;   
  17.   outlinenone; }   
  18.   .mui-switch:before {   
  19.     content'';   
  20.     width29px;   
  21.     height29px;   
  22.     positionabsolute;   
  23.     top0px;   
  24.     left: 0;   
  25.     border-radius: 20px;   
  26.     border-top-left-radius: 20px;   
  27.     border-top-rightright-radius: 20px;   
  28.     border-bottom-left-radius: 20px;   
  29.     border-bottom-rightright-radius: 20px;   
  30.     background-color#fff;   
  31.     box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); }   
  32.   .mui-switch:checked {   
  33.     border-color#64bd63;   
  34.     box-shadow: #64bd63 0 0 0 16px inset;   
  35.     background-color#64bd63; }   
  36.     .mui-switch:checked:before {   
  37.       left21px; }   
  38.   .mui-switch.mui-switch-animbg {   
  39.     transition: background-color ease 0.4s; }   
  40.     .mui-switch.mui-switch-animbg:before {   
  41.       transition: left 0.3s; }   
  42.     .mui-switch.mui-switch-animbg:checked {   
  43.       box-shadow: #dfdfdf 0 0 0 0 inset;   
  44.       background-color#64bd63;   
  45.       transition: border-color 0.4s, background-color ease 0.4s; }   
  46.       .mui-switch.mui-switch-animbg:checked:before {   
  47.         transition: left 0.3s; }   
  48.   .mui-switch.mui-switch-anim {   
  49.     transition: border cubic-bezier(0, 0, 0, 1) 0.4s, box-shadow cubic-bezier(0, 0, 0, 1) 0.4s; }   
  50.     .mui-switch.mui-switch-anim:before {   
  51.       transition: left 0.3s; }   
  52.     .mui-switch.mui-switch-anim:checked {   
  53.       box-shadow: #64bd63 0 0 0 16px inset;   
  54.       background-color#64bd63;   
  55.       transition: border ease 0.4s, box-shadow ease 0.4s, background-color ease 1.2s; }   
  56.       .mui-switch.mui-switch-anim:checked:before {   
  57.         transition: left 0.3s; }   
  58.     
  59. /*# sourceMappingURL=mui-switch.css.map */  

SCSS代码:

CSS Code复制内容到剪贴板
  1. @mixin borderRadius($radius:20px) {   
  2.   border-radius: $radius;   
  3.   border-top-left-radius: $radius;   
  4.   border-top-rightright-radius: $radius;   
  5.   border-bottom-left-radius: $radius;   
  6.   border-bottom-rightright-radius: $radius;   
  7. }   
  8.     
  9. $duration: .4s;   
  10. $checkedColor: #64bd63;   
  11. .mui-switch {   
  12.   width52px;   
  13.   height31px;   
  14.   positionrelative;   
  15.   border1px solid #dfdfdf;   
  16.   background-color#fdfdfd;   
  17.   box-shadow: #dfdfdf 0 0 0 0 inset;   
  18.   @include borderRadius();   
  19.   background-clipcontent-box;   
  20.   displayinline-block;   
  21.   -webkit-appearance: none;   
  22.   user-select: none;   
  23.   outlinenone;   
  24.   &:before {   
  25.     content'';   
  26.     width29px;   
  27.     height29px;   
  28.     positionabsolute;   
  29.     top0px;   
  30.     left: 0;   
  31.     @include borderRadius();   
  32.     background-color#fff;   
  33.     box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);   
  34.   }   
  35.   &:checked {   
  36.     border-color: $checkedColor;   
  37.     box-shadow: $checkedColor 0 0 0 16px inset;   
  38.     background-color: $checkedColor;   
  39.     &:before {   
  40.       left21px;   
  41.     }   
  42.   }   
  43.   &.mui-switch-animbg {   
  44.     transition: background-color ease $duration;   
  45.     &:before {   
  46.       transition: left 0.3s;   
  47.     }   
  48.     &:checked {   
  49.       box-shadow: #dfdfdf 0 0 0 0 inset;   
  50.       background-color: $checkedColor;   
  51.       transition: border-color $duration, background-color ease $duration;   
  52.       &:before {   
  53.         transition: left 0.3s;   
  54.       }   
  55.     }   
  56.     
  57.   }   
  58.   &.mui-switch-anim {   
  59.     transition: border cubic-bezier(0, 0, 0, 1) $duration, box-shadow cubic-bezier(0, 0, 0, 1) $duration;   
  60.     &:before {   
  61.       transition: left 0.3s;   
  62.     }   
  63.     &:checked {   
  64.       box-shadow: $checkedColor 0 0 0 16px inset;   
  65.       background-color: $checkedColor;   
  66.       transition: border ease $duration, box-shadow ease $duration, background-color ease $duration*3;   
  67.       &:before {   
  68.         transition: left 0.3s;   
  69.       }   
  70.     }   
  71.     
  72.   }   
  73. }  
HTML / CSS 相关文章推荐
Web前端绘制0.5像素的几种方法
Aug 11 HTML / CSS
css3设置box-pack和box-align让div里面的元素垂直居中
Sep 01 HTML / CSS
HTML5时代CSS设置漂亮字体取代图片
Sep 04 HTML / CSS
详解利用css3的var()实现运行时改变scss的变量值
Mar 02 HTML / CSS
HTML5中FileReader接口使用方法实例详解
Aug 26 HTML / CSS
HTML5等待加载动画效果
Jul 27 HTML / CSS
把富文本的回车转为br标签
Aug 09 HTML / CSS
html5开发之viewport使用
Oct 17 HTML / CSS
HTML5中的autofocus(自动聚焦)属性介绍
Apr 23 HTML / CSS
HTML5 Video标签的属性、方法和事件汇总介绍
Apr 24 HTML / CSS
基于canvas的骨骼动画的示例代码
Jun 12 HTML / CSS
html5 横向滑动导航栏的方法示例
May 08 HTML / CSS
基础的CSS3弹性盒Flexbox布局使用实例
Apr 08 #HTML / CSS
CSS3的Flexbox布局的简明入门指南
Apr 08 #HTML / CSS
使用CSS3设计地图上的雷达定位提示效果
Apr 05 #HTML / CSS
简单掌握CSS3中resize属性的用法
Apr 01 #HTML / CSS
CSS3制作炫酷的自定义发光文字
Mar 28 #HTML / CSS
可自定义箭头样式的CSS3气泡提示框
Mar 16 #HTML / CSS
6种非常炫酷的CSS3按钮边框动画特效
Mar 16 #HTML / CSS
You might like
PHP 工厂模式使用方法
2010/05/18 PHP
PHP安全配置详细说明
2011/09/26 PHP
JS中encodeURIComponent函数用php解码的代码
2012/03/01 PHP
PHP请求Socket接口测试实例
2016/08/12 PHP
lnmp安装多版本PHP共存的方法详解
2018/08/02 PHP
为JS扩展Array.prototype.indexOf引发的问题探讨及解决
2013/04/24 Javascript
JS获取几种URL地址的方法小结
2014/02/26 Javascript
jQuery获得指定元素坐标的方法
2015/04/14 Javascript
详解vue2.0组件通信各种情况总结与实例分析
2017/03/22 Javascript
Angularjs单选框相关的示例代码
2017/08/17 Javascript
vue.js语法及常用指令
2017/10/29 Javascript
解决Vue2.x父组件与子组件之间的双向绑定问题
2018/03/06 Javascript
解决vue-router进行build无法正常显示路由页面的问题
2018/03/06 Javascript
Layui实现数据表格默认全部显示(不要分页)
2019/10/26 Javascript
Vuex的实战使用详解
2019/10/31 Javascript
[08:47]2018国际邀请赛 OG战队举杯时刻
2018/08/29 DOTA
python网络编程学习笔记(10):webpy框架
2014/06/09 Python
使用Python下载歌词并嵌入歌曲文件中的实现代码
2015/11/13 Python
基于数据归一化以及Python实现方式
2018/07/11 Python
python数据结构学习之实现线性表的顺序
2018/09/28 Python
详解python3 + Scrapy爬虫学习之创建项目
2019/04/12 Python
python实现可变变量名方法详解
2019/07/01 Python
在python中画正态分布图像的实例
2019/07/08 Python
基于Django实现日志记录报错信息
2019/12/17 Python
Tensorflow中tf.ConfigProto()的用法详解
2020/02/06 Python
Keras自动下载的数据集/模型存放位置介绍
2020/06/19 Python
定义css设备类型-Media Queries图表简介及使用方法
2013/01/21 HTML / CSS
美国嘻哈文化生活方式品牌:GLD
2018/04/15 全球购物
eDreams葡萄牙:全球最大的在线旅行社之一
2019/04/15 全球购物
中国央视网签名寄语
2014/01/18 职场文书
领导党性分析材料
2014/02/15 职场文书
安全目标管理责任书
2014/07/25 职场文书
2014年社区工作总结
2014/11/18 职场文书
2015年加油站工作总结
2015/05/13 职场文书
Python编程源码报错解决方法总结经验分享
2021/10/05 Python
Mysql开启外网访问
2022/05/15 MySQL