vue自定义popView

2019-10-28  本文已影响0人  HCL黄

上一章我们已经说了如何自定义列表,今天这章我们继续来说一下如何自定义弹框,如下图

123.gif

直接上代码,一点一点分析解释

<template>
    <view v-if="showSelf" class="popV">
        <view class="popV-mask" @touchmove.stop.prevent="moveHandle"></view>
        <view class="popV-content" v-bind:style="{top:contentTop+'px'}">
            <slot></slot>
            <view class="footer">
                <view class="listV" v-for="(item, index) in footers" :key="index" :class='[footers.length>1&&index!=footers.length-1?"listV-rightBorder":""]'
                 @click="didClickItem(index)" :hover-class="footers.length==1?'hover-left-right' :footers.length>1&&index==0?'hover-left' :index==footers.length-1?'hover-right':'hover-center'">
                    <text class="listV-text" :class="index==footers.length-1 ? 'text-highlighted':''">{{item}}</text>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
    export default {
        props: {
            //弹窗组件是否显示 默认不显示 必传属性
            show: {
                type: Boolean,
                default: false,
                required: true,
            },
            footerLists: {
                typs: Array,
                default: [],
                required: false,
            },
            pContentTop: {
                type: Number,
                default: 185, // 单位px
                required: false
            }
        },
        data() {
            return {
                showSelf: false,
                footers: ['取消', '确定'],
                contentTop: 185 // 单位px
            }
        },
        watch: {
            show(val) {
                var that = this
                if (!val) {
                    that.closeMyself()
                } else {
                    that.showSelf = val
                }
            },
            footerLists(array) {
                this.footers = array;
            }
        },
        created() {
            this.showSelf = this.show;
            this.contentTop = this.pContentTop;
            this.footers = this.footerLists;
        },
        mounted() {
            // this.showSelf = this.show;
            // this.contentTop = this.pContentTop;
            // this.footers = this.footerLists;
        },
        methods: {
            moveHandle: function(){
                
            },
            /** 底部按钮操作 */
            didClickItem(index) {
                this.$emit('itemClick', index);
            },
            /** 点击遮罩关闭弹窗 */
            closeMyself(event) {
                this.showSelf = false;
            }
        }
    }
</script>
<style>
    .popV {
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .popV-mask {
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.4);
        display: block;
        position: fixed;
        z-index: 1000;
    }

    .popV-content {
        /* top: 370upx; */
        width: 606upx;
        /* height: 528upx; */
        position: fixed;
        left: 72upx;
        z-index: 1001;

        display: flex;
        flex-direction: column;
        align-items: center;
        background-color: #FFFFFF;
        border-radius: 10upx;
    }

    .footer {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        bottom: 0;
        left: 0;
        height: 86upx;
        width: 100%;

        border-top-style: solid;
        border-top-color: #E1E1E1;
        border-top-width: 1upx;
    }

    .listV {
        display: flex;
        align-items: center;
        justify-content: center;
        flex: 1;
    }

    .active {
        background-color: #DDDDDD;
    }

    .listV-rightBorder {
        border-right-style: solid;
        border-right-color: #E1E1E1;
        border-right-width: 1upx;
    }

    .listV-text {
        font-family: PingFangSC-Regular;
        color: #787878;
        font-size: 30upx;
    }

    .text-highlighted {
        color: #3ABBFF;
    }

    .hover-left-right {
        background: #DDD;
        border-bottom-left-radius: 10upx;
        border-bottom-right-radius: 10upx;
    }

    .hover-left {
        background: #DDD;
        border-bottom-left-radius: 10upx;
    }

    .hover-right {
        background: #DDD;
        border-bottom-right-radius: 10upx;
    }

    .hover-center {
        background: #DDD;
    }
</style>

如何使用

第一步导入
import popView from "@/components/popView.vue"
components: {
    popView
}
第二步创建
7F16CCB7-9A7A-4E90-BE8A-9F4AD3166E84.png
    /* 通用弹框 */
    .singlePopV-header {
        display: flex;
        flex-direction: column;
        width: 100%;
        align-items: center;
    }

    .singlePopV-header-content {
        font-family: PingFangSC-Regular;
        color: #333333;
        font-size: 28upx;
        line-height: 40upx;
        margin: 40upx;
        text-align: center;
    }
第三步初始化默认值
F70B6845-8DD9-47C5-9B7C-0A6B14CFEFE8.png
第四步控制显示隐藏
A0BD19F1-1C03-41F6-9A65-062D354F668D.png
第五步监听按钮点击
2E206B21-D7E2-4047-B35F-435F9900F634.png
上一篇下一篇

猜你喜欢

热点阅读