基于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" />
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是一款流行的JavaScript框架,它在构建用户界面方面非常强大。以下是一个示例代码,用于封装一个基本的弹窗组件: ```javascript <template> <div> <button @click="showModal">打开弹窗</button> <div v-if="isOpen" class="modal"> <div class="modal-content"> <slot></slot> <button @click="closeModal">关闭弹窗</button> </div> </div> </div> </template> <script> import { ref } from 'vue'; export default { name: 'Modal', setup() { const isOpen = ref(false); const showModal = () => { isOpen.value = true; }; const closeModal = () => { isOpen.value = false; }; return { isOpen, showModal, closeModal }; }, }; </script> <style scoped> .modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; } .modal-content { background-color: white; padding: 20px; } button { margin-top: 10px; } </style> ``` 这个弹窗组件包含了一个按钮和一个模态框。当点击按钮时,模态框会显示出来,并且渲染出插槽内容。关闭按钮可以用于关闭模态框。在模态框外部点击也可以关闭模态框。 这个组件使用Vue3的Composition API来定义逻辑。通过ref函数创建一个响应式引用isOpen,用于跟踪模态框的开启和关闭状态。showModal方法用于打开模态框,closeModal方法用于关闭模态框。使用slot插槽来动态渲染弹窗内容。 在样式上,将模态框设置为固定定位,并使用背景色来实现半透明遮罩效果。模态框内容使用白色背景并设置内边距。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值