基于vue封装一个公用弹窗

<template>
  <!-- 公用弹窗封装 -->
  <div class="popupOuter">
    <transition name="leave">
      <div class="popup" v-if="show"></div>
    </transition>
    <transition name="bounce">
      <div class="popupMain" v-if="show">
        <!-- <div class="popupMain"> -->
        <!-- 弹窗主要内容 -->
        <slot></slot>
        <div
          class="popupClose"
          @click="closePopup"
          v-if="isShowCloseIcon"
        ></div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: "Popup",
  props: {
    // 控制弹窗显隐
    parPopup: {
      type: Boolean,
      default: false,
    },
    // 是否显示底部关闭按钮
    isShowCloseIcon: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      show: false,
    };
  },
  created() {},
  methods: {
    //   关闭弹窗
    closePopup() {
      this.$emit("update:parPopup", false);
    },
  },
  watch: {
    parPopup(newVal) {
      this.show = newVal;
      if (newVal === true) {
        // 弹窗打开时给body添加类名阻止穿透滑动底部页面
        document.body.classList.add("modal-open");
      } else {
        document.body.classList.remove("modal-open");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.popup {
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.7);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 2000;
}
//   vant toast组件z-inde未2002
.popupMain {
  position: relative;
  z-index: 2001;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 6rem;
  height: 6rem;
  background: #fff;

  .popupClose {
    width: 0.75rem;
    height: 0.75rem;
    background: url(https://vncdn.mobi88.cn/fjjh_lkxrl/images/ct0804/popupClose.png)
      no-repeat;
    background-size: contain;
    margin: 0.63rem auto 0;
  }
}

// 弹窗动画封装
.bounce-enter-active {
  -webkit-animation: bounce-in 0.3s cubic-bezier(0, 0, 0.65, 1.75) 1;
}
.bounce-leave-active {
  -webkit-animation: bounce-in 0.3s cubic-bezier(0, 0, 0.65, 1.75) reverse;
}
@-webkit-keyframes bounce-in {
  0% {
    transform: scale(0);
    // 开启硬件加速
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
// 给最外朦层做一个延迟.3s退出的动画。
.leave-enter-active {
  -webkit-animation: bounce-out 0.2s cubic-bezier(0, 0, 0.65, 1.75) reverse;
}
.leave-leave-active {
  -webkit-animation: bounce-out 0.2s cubic-bezier(0, 0, 0.65, 1.75) 0.2s;
}
@-webkit-keyframes bounce-out {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
</style>

使用方式:

<popup :parPopup.sync="popup" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值