微信小程序----switch组件实现

WXRUI体验二维码

WXRUI体验码

如果文章对你有帮助的话,请打开微信扫一下二维码,点击一下广告,支持一下作者!谢谢!

效果图

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';
}));

注意

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

WXRUI体验二维码

WXRUI体验码

如果文章对你有帮助的话,请打开微信扫一下二维码,点击一下广告,支持一下作者!谢谢!

其他

我的博客,欢迎交流!

我的CSDN博客,欢迎交流!

微信小程序专栏

前端笔记专栏

微信小程序实现部分高德地图功能的DEMO下载

微信小程序实现MUI的部分效果的DEMO下载

微信小程序实现MUI的GIT项目地址

微信小程序实例列表

前端笔记列表

游戏列表

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rattenking

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值