vue自定义modal弹框

在vue中自定义modal弹出框组件,通过slot插槽的方式,动态注入中间body和底部内容。

<template>
    <div class="frame" ref="model" :style="{top: `${offset.top}px`, left: `${offset.left}px`}" v-if="showModel">
        <div class="frame-head" @mousedown="mousedown" @mousemove="mousemove" @mouseup="mouseup" @mouseleave="mouseleave">
            <div class="title">图片预览</div>
            <div class="close" @click="clickClose">×</div>
        </div>
         <div class="frame-info">
      		<slot name="info"></slot>
    	</div>
    	<div class="frame-btn">
     		<slot name="btn"></slot>
   		</div>
    </div>
</template>

<script>
    export default {
        name: "CustModel",
        data() {
            return {
                moveFlag: false,
                showModel: false,
                image: '',
                offset: {
                    x: 0,
                    y: 0,
                    left: 0,
                    top: 0
                }
            }
        },
        methods: {
            clickClose() {
                this.showModel = false;
                this.offset = {
                    x: 0,
                    y: 0,
                    left: 0,
                    top: 0
                }
            },
            openModel() {
                this.showModel = true;
            },
            mousedown(e) {
                this.moveFlag = true;
                this.offset.x = e.pageX - this.$refs.model.offsetLeft;
                this.offset.y = e.pageY - this.$refs.model.offsetTop;
            },
            mousemove(e) {
                if (this.moveFlag) {
                    this.offset.top = e.pageY - this.offset.y;
                    this.offset.left = e.pageX - this.offset.x;
                }
            },
            mouseleave() {
                this.moveFlag = false;
            },
            mouseup() {
                this.moveFlag = false;
            }
        }
    }
</script>

<style lang="less" scoped>
    .frame {
        position: fixed;
        width: 450px;
        height: auto;
        z-index: 1000;
        background: #ffffff;
        box-shadow: 0 0 4px 1px #a4a4a4;
        overflow: hidden;
        .frame-head {
            width: 100%;
            height: 40px;
            display: flex;
            background: #fff;
            border-bottom: 1px solid #a4a4a4;
            justify-content: space-between;
            padding: 0 10px;
            line-height: 40px;
            .title {
                font-weight: bold;
            }
            div {
                height: 100%;
            }
            .close {
                font-size: 20px;
                font-weight: bold;
                cursor: default;
            }
        }
        .frame-info {
            width: 100%;
            height: auto;
            background: #FFF;
            padding: 5px;
        }
        .frame-btn {
            width: 100%;
            height: auto;
            background: #FFF;
            padding: 5px;
        }
    }
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值