手写 el-dialog 弹窗效果

组件部分, 内容为空, 自己diy就行了, html 你难道不会? slot 插槽你难道不会?

复制代码
<template>
  <transition name="dialog-fade">
    <div v-if="modelValue" class="m-overly" @click="hideDialog">
      <div class="m-dialog-container" @click.stop></div>
    </div>
  </transition>
</template>

<script setup>
import { watch } from "vue";

const props = defineProps(["modelValue"]);
const emit = defineEmits(["update:modelValue"]);

function hideDialog() {
  emit("update:modelValue", false);
}

watch(
  () => props.modelValue,
  () => {
    console.log("变化了");
  }
);
</script>

<style lang="scss" scoped>
.m-overly {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: -20px;
  z-index: 100;
  background-color: rgba(0, 0, 0, 0.28);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;

  .m-dialog-container {
    width: 800px;
    height: 600px;
    border-radius: 20px;
    background: #fff;
  }
}

// 下面四个类是直接从element-plus搬过来的, 动画自己diy就行
.dialog-fade-enter-active {
  animation: dialog-fade-in 0.3s;
}

.dialog-fade-leave-active {
  animation: dialog-fade-out 0.3s;
}

@keyframes dialog-fade-in {
  0% {
    transform: translate3d(0, -20px, 0);
    opacity: 0;
  }
  100% {
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
}

@keyframes dialog-fade-out {
  0% {
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
  100% {
    transform: translate3d(0, -20px, 0);
    opacity: 0;
  }
}
</style>
复制代码

直接在页面上调用就行

复制代码
<template>
  <m-dialog v-model="showDialog"></m-dialog>

  <button @click="changeShow">切换显示隐藏</button>
</template>

<script setup>
import MDialog from "@/components/Dialog/Dialog.vue";
import { ref } from "vue";

const showDialog = ref(false);

function changeShow() {
  showDialog.value = !showDialog.value;

  console.log("执行了", showDialog.value);
}
</script>

<style lang="scss" scoped></style>
复制代码

 

posted @   深海里的星星i  阅读(463)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示