zoukankan      html  css  js  c++  java
  • 入坑uni-app受不了自有组件极差的体验便自己写了个switch组件

    uni-app坑不少,修改switch组件的原有样式在浏览器上能正常显示,但到了打包成APP后却不起作用;使用提供的方法style="transform:scale()只能按比例缩放,可定制性太差!这才打起自己写组件的念头,源码如下:

    <template>
        <view>
            <view class="weui-switch" :class="{'weui-switch-on' : isChecked}" :value="value" @tap="toggle"></view>
        </view>
    </template>
    <script>
        export default {
            name: 'myswitch',
            props: {
              value: {
                type: Boolean,
                default: true
              }
            },
            data () {
                return {
                    isChecked: this.value
                }
            },
            watch: {
              value (val) {
                this.isChecked = val
              },
              isChecked(val) {
                this.$emit('change', val);
              }
            },
            methods: {
              toggle() {
                this.isChecked = !this.isChecked;
              }
            }
        }    
    </script>
    <style>
        .weui-switch {
            display: block;
            position: relative;
             94rpx;
            height: 45rpx;
            outline: 0;
            border-radius: 30rpx;
            box-sizing: border-box;
            background-color: #DFDFDF;
            transition: background-color 0.1s, border 0.1s;
            cursor: pointer;
        }
        .weui-switch:before {
            content: " ";
            position: absolute;
            top: 0;
            left: 0;
             94rpx;
            height: 45rpx;
            border-radius: 30rpx;
            background-color: #3D424F;
            transition: transform 0.35s cubic-bezier(0.45, 1, 0.4, 1);
        }
        .weui-switch:after {
            content: " ";
            position: absolute;
            top: 2rpx;
            left: 4rpx;
             40rpx;
            height: 40rpx;
            border-radius: 50%;
            background-color: #FFFFFF;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
            transition: transform 0.35s cubic-bezier(0.4, 0.4, 0.25, 1.35);
        }
        .weui-switch-on {
            border-color: #6F6F6F;
            background-color: #00F8E9;
        }
        .weui-switch-on:before {
            border-color: #1AAD19;
            background-color: #00F8E9;
        }
        .weui-switch-on:after {
            transform: translateX(48rpx);
        }
    </style>

    引入方法:通过绑定change事件监听组件的开关状态

     <myswitch v-model="item.state" @change="mywitch(item)"></myswitch>
  • 相关阅读:
    转: MySQL 赋予用户权限(grant %-远程和localhost-本地区别)
    修改Apache的最大连接数
    正向代理与反向代理的区别【Nginx读书笔记】
    mysql加单引号和不加单引号的性能比较
    mysql保存数据提示1366 Incorrect string value: ‘xF0x9Fx98x8AxF0x9F…’ 解决
    Mysql外键约束设置使用方法
    MYSQL分库分表和不停机更改表结构
    Hive SQL 常用日期
    CountDownLatch学习
    Exchanger学习
  • 原文地址:https://www.cnblogs.com/bushan/p/12145336.html
Copyright © 2011-2022 走看看