有IOS的开关模拟,当然也有MIUI的开关模拟

看到设置选项里面的开关样式,突发奇想地来试试

  

最终效果如图:

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<style>
.switch-wrap {
position: relative;
margin: 50px auto;
width: 120px;
height: 40px;
font: 14px/1.5 Arial, Sans-Serif;
} .switch,
.switch input,
.switch input:before {
width: 30px;
height: 14px;
} .switch input {
position: absolute;
right: 0;
} .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;
} .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;
} .switch input:checked:after {
left: 15px;
background-color: #36a6fa;
transition: .2s left, .2s background-color;
}
</style> <script src="jquery.js"></script>
<script type="text/javascript">
$('#switch').change(function() {
$('.switch-action').text(this.checked ? '关闭' : '开启');
});
</script>
</head> <body> <div class="switch-wrap">
<span><span class="switch-action">开启</span>WLAN</span>
<label class="switch">
<input type="checkbox" name="switch" id="switch">
</label>
</div>
</body> </html>

  

实现过程

1. 选项框checkbox

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

2. 理解开关的过程

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

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

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

  <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伪元素作为开关项(即那个小圆圈)

        .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;
}
        .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;
}

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

        .switch input:checked:after {
left: 15px;
background-color: #36a6fa;
transition: .2s left, .2s background-color;
}

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

 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检测一下开关的状态变化

    <script src="jquery.js"></script>
<script type="text/javascript">
$('#switch').change(function() {
$('.switch-action').text(this.checked ? '关闭' : '开启');
});
</script>
[-_-]眼睛累了吧,注意劳逸结合呀[-_-]

IOS开关效果的更多相关文章

  1. 自己定义控件:onDraw 方法实现仿 iOS 的开关效果

    概述 本文主要解说怎样在 Android 下实现高仿 iOS 的开关按钮,并不是是在 Android 自带的 ToggleButton 上改动,而是使用 API 提供的 onDraw.onMeasur ...

  2. android 仿ios开关控件

    ios一些控件还是挺美丽的,可是对android程序猿来说可能比較苦逼,由于ios一些看起来简单的效果对android来说可能就没那么简单了,可是没办法非常多产品都是拿ios的一些控件叫android ...

  3. 【转】提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果

    原文网址:http://www.zhimengzhe.com/IOSkaifa/37910.html MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单.方便,并且可以对显 ...

  4. Android仿IOS回弹效果 ScrollView回弹 总结

    Android仿IOS回弹效果  ScrollView回弹 总结 应项目中的需求  须要仿IOS 下拉回弹的效果 , 我在网上搜了非常多 大多数都是拿scrollview 改吧改吧 试了一些  发现总 ...

  5. Android 实现高仿iOS桌面效果之可拖动的GridView(上)

    转载请标明出处:http://blog.csdn.net/sk719887916/article/details/40074663,作者:skay      最近项目中遇到一个LIstview的拖动效 ...

  6. iOS 波浪效果的实现

    iOS 波浪效果的实现 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4f8187; background-c ...

  7. 【iOS系列】- iOS吸附效果的实现 之 UICollectionView的使用全解

    [iOS系列]- iOS吸附效果的实现 之 UICollectionView的使用全解 UICollectionView可以做很多的布局,在iOS开发中较为重要,所以这里就以实例来讲解UICollec ...

  8. iOS 复选框风格转换 Switchery 开关效果

    Switchery 是个简单的 JavaScript 组件,只要几个简单的步骤就可以帮助用户把默认的 HTML 复选框转换成漂亮 iOS 7 样式风格.用户可以很方便的自定义这种转换,所以可以很好的配 ...

  9. js模仿ios select效果

    github:https://github.com/zhoushengmufc/iosselect webapp模仿ios下拉菜单 html下拉菜单select在安卓和IOS下表现不一样,iossel ...

随机推荐

  1. MemCached缓存操作

    Web项目在运行时,通常需要从数据库中进行读写.随着操作数据量的增大,以及访问量的集中,数据库的负载增加,数据库响应变慢,网站访问速度变慢的情况.Memcached就是用来解决这些问题的. Memca ...

  2. Laravel 中自定义日志目录

    参考:https://laravel-china.org/articles/7125/custom-log-directory-in-laravel

  3. php 高并发解决方案(用于抢购) 转载

    最近在做一个团购项目,遇到个问题,就是在抢购.秒杀.抽奖等活动时,库存数量有限,但是同时下单人数超过了库存数量,就会导致商品超售问题.那么我们怎么来解决这个问题呢,我的思路如下: sql1:查询商品库 ...

  4. ifconfig与内核通信 ifreq 结构体分析和使用

    结构原型: /* * Interface request structure used for socket * ioctl's.  All interface ioctl's must have p ...

  5. Robust Online Visual Tracking with a Single Convolutional Neural Network

    Abstract:这篇论文有三个贡献,第一提出了新颖的简化的结构损失函数,能保持尽量多的训练样本,通过适应模型输出的不确定性来减少跟踪误差累积风险. 第二是增强了普通的SGD,采用了暂时的选择策略来进 ...

  6. POJ 2513 Colored Sticks(Tire+欧拉回(通)路判断)

    题目链接:http://poj.org/problem?id=2513 题目大意:你有好多根棍子,这些棍子的两端分都别涂了一种颜色.请问你手中的这些棍子能否互相拼接,从而形成一条直线呢? 两根棍子只有 ...

  7. CSS&&xpath

    1.元素选择器 <div class="wrap"> 直接选择文档元素div 2.类选择器 <div class="wrap"> 元素的 ...

  8. jmeter-----GUI运行和非GUI运行的区别

    gui:界面会消耗很多资源,并且运行的结果是保存在Jmeter运行的内存中.当时间一长,内存增长到一定程度,就会报错,甚至假死. 非gui:实时的将运行log文件保存到本地文件中,不会撑爆内存.并且对 ...

  9. ref:学习笔记 UpdateXml() MYSQL显错注入

    ref:https://www.cnblogs.com/MiWhite/p/6228491.html 学习笔记 UpdateXml() MYSQL显错注入 在学习之前,需要先了解 UpdateXml( ...

  10. 20169211《Linux内核原理与分析》第三周作业

    假期中抽时间学习了一下linux内核的启动过程,在此做一下学习总结. Linux启动过程描述: 1.启动BootLoader 2.Linux系统的初始化 3.Linux的应用程序的初始化 通用寄存器的 ...