前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MIUI选项框开关样式模拟

MIUI选项框开关样式模拟

作者头像
书童小二
发布2018-09-03 15:54:03
8240
发布2018-09-03 15:54:03
举报
文章被收录于专栏:前端儿前端儿

有IOS的开关模拟,当然也有MIUI的开关模拟 看到设置选项里面的开关样式,突发奇想地来试试

最终效果如图:

实现过程

1. 选项框checkbox 

模拟开关当然需要一个选项框,这里用到了复选框checkbox

2. 理解开关的过程

点击开关按钮,则开启或关闭。原生的checkbox无法做到图示的效果,所以就需要额外的元素来表示图中的开关

而我们又要使用到checkbox的点击效果以及点击后是否选中(checked)的效果,所以checkbox不能隐藏,但可以用覆盖的方式

为了减少多余标签的使用,可以使用伪元素:before、:after ,标签结构为

代码语言:javascript
复制
  <div class="switch-wrap">
        <span><span class="switch-action">开启</span>WLAN</span>
        <label class="switch">
            <input type="checkbox" name="switch" id="switch">
        </label>
    </div>
3. 开关的实现

用:before伪元素作为开关背景层,用:after伪元素作为开关项(即那个小圆圈)

代码语言:javascript
复制
        .switch input:before {
            content: '';
            display: inline-block;
            position: relative;
            border-radius: 20px;
            border: 1px solid #ccccc6;
            box-shadow: 0 0 1px 1px #ececf3;
            background-color: #fff;
            cursor: pointer;
        }
代码语言:javascript
复制
        .switch input:after {
            content: '';
            position: absolute;
            width: 12px;
            height: 12px;
            top: 2px;
            left: 3px;
            border-radius: 50%;
            background-color: #ccccc6;
            transition: .2s left, .2s background-color;
        }

初始小圆圈在左侧,当开关状态为开启时,右移,并更新开启状态的背景色

代码语言:javascript
复制
        .switch input:checked:after {
            left: 15px;
            background-color: #36a6fa;
            transition: .2s left, .2s background-color;
        }

以上就是关键的代码了,以下为完整的样式

代码语言:javascript
复制
 1 <style>
 2     .switch-wrap {
 3             position: relative;
 4             margin: 50px auto;
 5             width: 120px;
 6             height: 40px;
 7             font: 14px/1.5 Arial, Sans-Serif;
 8         }
 9 
10         .switch,
11         .switch input,
12         .switch input:before {
13             width: 30px;
14             height: 14px;
15         }
16 
17         .switch input {
18             position: absolute;
19             right: 0;
20         }
21 
22         .switch input:before {
23             content: '';
24             display: inline-block;
25             position: relative;
26             border-radius: 20px;
27             border: 1px solid #ccccc6;
28             box-shadow: 0 0 1px 1px #ececf3;
29             background-color: #fff;
30             cursor: pointer;
31         }
32 
33         .switch input:after {
34             content: '';
35             position: absolute;
36             width: 12px;
37             height: 12px;
38             top: 2px;
39             left: 3px;
40             border-radius: 50%;
41             background-color: #ccccc6;
42             transition: .2s left, .2s background-color;
43         }
44 
45         .switch input:checked:after {
46             left: 15px;
47             background-color: #36a6fa;
48             transition: .2s left, .2s background-color;
49         }
50 
51 
52     </style>
4. 开关的测试

最后,可结合JS检测一下开关的状态变化

代码语言:javascript
复制
    <script src="jquery.js"></script>
    <script type="text/javascript">
        $('#switch').change(function() {
            $('.switch-action').text(this.checked ? '关闭' : '开启');
        });
    </script>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-09-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现过程
    • 1. 选项框checkbox 
      • 2. 理解开关的过程
        • 3. 开关的实现
          • 4. 开关的测试
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档