Vue封装弹窗

26 篇文章 0 订阅
17 篇文章 1 订阅

1.创建 EXDialog.vue 文件 (代码很简单 没有注释 如有不懂 评论就好)

<template>
  <transition name="fade">
    <div :style="style" class="ex-dialog__wrapper" v-show="visible">
      <div>
        <div>
          <div class="ex-height-info"><span>{{ title }}</span>
            <div @click="$emit('update:visible',false)">X</div>
          </div>
        </div>
        <div class="color_body">
          <slot></slot>
        </div>
      </div>

    </div>
  </transition>
</template>

<script>
import logDing from "@/components/common/Dialog/ex_dialog";
export default {
  name: "EXDialog",
  props: {
    width: String,
    title: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    "visible" : function (val) {
      if(val) {
        // 控制背景的显示喝隐藏
        logDing(this)
      }
    }
  },
  computed: {
    style() {
      let style = {};
      if (this.width) {
        style.width = this.width;
      }
      return style;
    }
  },
  data() {
    return {}
  },
}
</script>

<style lang="scss" scoped>

.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}

.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */
{
  opacity: 0;
}

.ex-dialog__wrapper {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1000;

  .ex-height-info {
    height: 48px;
    line-height: 48px;
    background: linear-gradient(181deg, #63A2FB, #68A1F3, #3A79D3);
    text-align: center;
    position: relative;

    span {
      font-size: 16px;
      color: #fff;
      font-weight: bold;
    }

    div {
      position: absolute;
      right: 20px;
      top: 0;
      cursor: pointer;
      font-size: 16px;
      color: #fff;
      font-weight: bold;
    }
  }
}

.color_body {
  background: #fff;
  border: 1px solid #DEDEDE;
}
</style>

  1. 创建ex_dialog.js 文件
function logDing(_this) {
  let body = document.querySelector("body")
  let div = document.createElement('div')
  div.style.position = "fixed"
  div.style.width = "100%"
  div.style.height = "100%"
  div.style.background = "#000"
  div.style.opacity = '0.5'
  div.style.top = '0'
  div.style.zIndex = '10'
  div.addEventListener("click",  () => {
    _this.$emit('update:visible',false)
    div.remove()
  }, false)
  body.append(div)
}

export default logDing

3.调用
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值