Vue3 之dialog弹框简单制作

一、效果图

二、代码

mydialog.vue

复制代码
<template>
    <div class="vux-x-dialog">
        <transition :name="maskTransition">
            <div class="weui-mask" @click.self="hide" v-show="show" :style="maskStyle">
                <transition :name="dialogTransition">
                    <div :class="dialogClass" v-show="show" :style="dialogStyle">
                        <slot></slot>
                    </div>
                </transition>
            </div>
        </transition>
    </div>
</template>

<script>
    export default {
        name: 'x-dialog',
        model: {
            prop: 'show',
            event: 'change'
        },
        props: {
            show: {
                type: Boolean,
                default: false
            },
            maskTransition: {
                type: String,
                default: 'vux-mask'
            },
            maskZIndex: [String, Number],
            dialogTransition: {
                type: String,
                default: 'vux-dialog'
            },
            dialogClass: {
                type: String,
                default: 'weui-dialog'
            },
            hideOnBlur: Boolean,
            dialogStyle: Object
        },
        computed: {
            maskStyle() {
                if (typeof this.maskZIndex !== 'undefined') {
                    return {
                        zIndex: this.maskZIndex
                    }
                }
            }
        },
        mounted() {

        },
        watch: {
            show(val) {
                this.$emit('update:show', val)
                this.$emit(val ? 'on-show' : 'on-hide')
            }
        },
        methods: {
            hide() {
                if (this.hideOnBlur) {
                    this.$emit('update:show', false)
                }
            }
        },
        data() {
            return {

            }
        }
    }
</script>

<style scoped>
    .weui-dialog {
        position: fixed;
        display: table;
        z-index: 5000;
        width: 80%;
        max-width: 300px;
        margin: auto;
        background-color: #fff;
        text-align: center;
        border-radius: 3px;
        overflow: hidden;
    }

    .weui-mask .weui-dialog {
        position: relative;
    }

    .weui-mask {
        position: fixed;
        display: flex;
        justify-content: center;
        align-items: center;
        z-index: 1000;
        top: 0;
        right: 0;
        left: 0;
        bottom: 0;
        background: rgba(0, 0, 0, .6);
    }

    .vux-x-dialog .weui-mask {
        background: rgba(0, 0, 0, .5);
        position: fixed;
        z-index: 1000;
    }

    .weui-mask_transparent {
        position: fixed;
        z-index: 1000;
        top: 0;
        right: 0;
        left: 0;
        bottom: 0;
    }

    .vux-fade-enter-active,
    .vux-fade-leave-active {
        opacity: 1;
        transition: opacity linear 0.2s;
    }

    .vux-fade-enter,
    .vux-fade-leave-to {
        opacity: 0;
    }

    .vux-dialog-enter-active {
        animation: vux-dialog-in .5s;
    }

    .vux-dialog-leave-active {
        animation: vux-dialog-out .3s;
    }

    @keyframes vux-dialog-in {
        0% {
            transform: scale(1.185);
            opacity: 0;
        }

        100% {
            transform: scale(1);
            opacity: 1;
        }
    }

    @keyframes vux-dialog-out {
        0% {
            transform: scale(1);
            opacity: 1;
        }

        100% {
            transform: scale(0.85);
            opacity: 0;
        }
    }

    .vux-mask-enter,
    .vux-mask-leave-active {
        opacity: 0;
    }

    .vux-mask-leave-active,
    .vux-mask-enter-active {
        transition: opacity 300ms;
    }
</style>

三、使用

复制代码
import XDialog from '@/components/MyDialog.vue';

const isshow = ref(false)
const handleShowUpdate = (val) => {
    isshow.value = val
}
const dShow = () => {
    alert('show')
}
const dHide = () => {
    alert('hide')
}

<x-dialog :show="isshow" :dialogStyle="{width: '27rem', maxWidth: '27rem', borderRadius: '8px'}" :hideOnBlur="true" @update:show="handleShowUpdate" @on-show="dShow" @on-hide="dHide">
    123
</x-dialog>
相关推荐
im_AMBER20 分钟前
React 01
前端·javascript·笔记·react.js·前端框架·web
@大迁世界26 分钟前
React 19.2.0 有哪些新变化
前端·javascript·react.js·前端框架·ecmascript
华仔啊1 小时前
用 Vue3 + Canvas 做了个超实用的水印工具,同事都在抢着用
前端·vue.js·canvas
炒毛豆2 小时前
uniapp微信小程序+vue3基础内容介绍~(含标签、组件生命周期、页面生命周期、条件编译(一码多用)、分包))
vue.js·微信小程序·uni-app
Bacon2 小时前
前端:从0-1实现一个脚手架
前端
Bacon2 小时前
前端项目部署实战 nginx+docker持续集成
前端
beckyye2 小时前
阿里云智能语音简单使用:语音识别
前端·语音识别·录音
东东2332 小时前
前端规范工具之husky与lint-staged
前端·javascript·eslint
jump6802 小时前
手写事件总线、事件总线可能带来的内存泄露问题
前端
岁月宁静3 小时前
在 Vue 3.5 中优雅地集成 wangEditor,并定制“AI 工具”下拉菜单(总结/润色/翻译)
前端·javascript·vue.js