微信小程序switch组件使用详解


Posted in Javascript onJanuary 31, 2018

本文实例为大家分享了微信小程序switch组件的实现代码,供大家参考,具体内容如下

效果图

微信小程序switch组件使用详解

HTML

<div class="switch-list">
 <span class="fui-fr">红色switch组件</span>
 <input class="fui-switch" style="color:rgb(255, 0, 0);" type="checkbox" checked>
</div>
<div class="switch-list">
 <span class="fui-fr">绿色switch组件</span>
 <input class="fui-switch" style="color:rgb(76, 216, 100);" type="checkbox" checked>
</div>
<div class="switch-list">
 <span class="fui-fr">绿色禁用switch组件</span>
 <input class="fui-switch" style="color:rgb(76, 216, 100);" type="checkbox" checked disabled>
</div>
<div class="switch-list">
 <span class="fui-fr">蓝色switch组件---开</span> 
 <input class="fui-switch" style="color:blue" type="checkbox" checked>
</div>
<div class="switch-list">
 <span class="fui-fr">蓝色switch组件---关</span> 
 <input class="fui-switch" style="color:blue" type="checkbox">
</div>

CSS

.switch-list{
 padding: .5rem;
}
.fui-switch{
 position: relative;
 width: .87rem;
 height: .5rem;
 z-index: 10;
 display: inline-block;
 outline: medium;
 border: 1px solid #dfdfdf;
 border-radius: .25rem;
 background-color: #dfdfdf;
 -webkit-appearance: none;
 -moz-appearance: none;
 vertical-align: middle;
}
.fui-switch:checked{
 border-color: currentColor;
 background-color: currentColor;
}
.fui-switch::after,.fui-switch::before{
 content: "";
 position: absolute;
 height: .44rem;
 top: 0;
 left: 0;
 border-radius: .25rem;
 -webkit-transition: -webkit-transform .3s;
 transition: -webkit-transform .3s;
 transition: transform .3s;
 transition: transform .3s,-webkit-transform .3s;
}
.fui-switch:before {
 width: .84rem;
 background-color: #fdfdfd;
}
.fui-switch:checked:before {
 -webkit-transform: scale(0);
 transform: scale(0);
}
.fui-switch:after {
 width: .44rem;
 background-color: #fff;
 box-shadow: 0 1px 3px rgba(0,0,0,.4);
}
.fui-switch:checked:after {
 -webkit-transform: translateX(.4rem);
 transform: translateX(.4rem);
}
.fui-switch[disabled] {
 opacity: .5;
}
.fui-fr{font-size: .3rem;vertical-align: middle;}

实现rem的JS

(function(win,factory){
 factory(win);
 window.addEventListener('resize',function(){factory(win)},false);
}(window,function(win){
 var width = document.documentElement.clientWidth;
 width = width > 750 ? 750 : width;
 document.documentElement.style.fontSize = width / 7.5 + 'px';
}));

注意

此处 1rem 在 750 的 psd 设计图代表 100px ;
switch 的切换动画是通过 CSS3 的 transition 属性实现;
主要是控制 switch 的 after 的移动,以及 before 的放大缩小动画。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript中两种链式调用实现代码
Jan 12 Javascript
node.js中的fs.writeSync方法使用说明
Dec 15 Javascript
深入解析JavaScript中的arguments对象
Jun 12 Javascript
浅析js的模块化编写 require.js
Dec 07 Javascript
利用canvas实现的加载动画效果实例代码
Jul 05 Javascript
vue实现页面加载动画效果
Sep 19 Javascript
微信小程序录音与播放录音功能
Dec 25 Javascript
javascript网页随机点名实现过程解析
Oct 15 Javascript
jQuery实现图片随机切换、抽奖功能(实例代码)
Oct 23 jQuery
js实现省级联动(数据结构优化)
Jul 17 Javascript
vue页面引入three.js实现3d动画场景操作
Aug 10 Javascript
Node实现搜索框进行模糊查询
Jun 28 Javascript
vue项目中导入swiper插件的方法
Jan 30 #Javascript
微信小程序实现手势图案锁屏功能
Jan 30 #Javascript
简单理解Vue中的nextTick方法
Jan 30 #Javascript
Vue组件中slot的用法
Jan 30 #Javascript
vue初尝试--项目结构(推荐)
Jan 30 #Javascript
JS实现的邮箱提示补全效果示例
Jan 30 #Javascript
JS实现的抛物线运动效果示例
Jan 30 #Javascript
You might like
两个开源的Php输出Excel文件类
2010/02/08 PHP
php和jquery实现地图区域数据统计展示数据示例
2014/02/12 PHP
php去除html标记的原生函数详解
2015/01/27 PHP
Thinkphp结合ajaxFileUpload实现异步图片传输示例
2017/03/13 PHP
基于jquery的图片幻灯展示源码
2012/07/15 Javascript
FireBug 调试JS入门教程 如何调试JS
2013/12/23 Javascript
JavaScript获取table中某一列的值的方法
2014/05/06 Javascript
jquery实现模拟百分比进度条渐变效果代码
2015/10/29 Javascript
给easyui datebox扩展一个清空的实例
2016/11/09 Javascript
JavaScript之面向对象_动力节点Java学院整理
2017/06/29 Javascript
vue基础之data存储数据及v-for循环用法示例
2019/03/08 Javascript
Preload基础使用方法详解
2020/02/03 Javascript
[01:50]《我与DAC》之玩家:iG夺冠时的那面红旗
2018/03/29 DOTA
python 简易计算器程序,代码就几行
2009/08/29 Python
浅谈python中scipy.misc.logsumexp函数的运用场景
2016/06/23 Python
Python下实现的RSA加密/解密及签名/验证功能示例
2017/07/17 Python
Python 判断是否为质数或素数的实例
2017/10/30 Python
好的Python培训机构应该具备哪些条件
2018/05/23 Python
python 美化输出信息的实例
2018/10/15 Python
在Python中过滤Windows文件名中的非法字符方法
2019/06/10 Python
python word转pdf代码实例
2019/08/16 Python
Python Pandas对缺失值的处理方法
2019/09/27 Python
python pygame实现挡板弹球游戏
2019/11/25 Python
Django使用rest_framework写出API
2020/05/21 Python
tensorflow实现从.ckpt文件中读取任意变量
2020/05/26 Python
python调用摄像头的示例代码
2020/09/28 Python
迪卡侬(Decathlon)加拿大官网:源自法国的运动专业超市
2020/11/22 全球购物
荷兰浴室和卫浴网上商店:Badkamerxxl.nl
2020/10/06 全球购物
利用promise及参数解构封装ajax请求的方法
2021/03/24 Javascript
CAD制图人员的自荐信
2014/02/07 职场文书
李培根演讲稿
2014/05/22 职场文书
自主招生自荐信怎么写
2015/03/24 职场文书
Golang 如何实现函数的任意类型传参
2021/04/29 Golang
MySQL外键约束(FOREIGN KEY)案例讲解
2021/08/23 MySQL
python3 字符串str和bytes相互转换
2022/03/23 Python
Python万能模板案例之matplotlib绘制甘特图
2022/04/13 Python