当前位置:首页 > VUE

vue自己实现模态框

2026-02-23 02:03:22VUE

实现模态框的基本结构

在Vue中实现模态框,可以通过组件化方式封装。创建一个Modal.vue文件,定义模态框的模板、样式和逻辑。模态框通常包含遮罩层、内容区域、关闭按钮等元素。

<template>
  <div class="modal-mask" v-show="visible" @click.self="close">
    <div class="modal-container">
      <div class="modal-header">
        <slot name="header"></slot>
        <button class="modal-close" @click="close">×</button>
      </div>
      <div class="modal-body">
        <slot name="body"></slot>
      </div>
      <div class="modal-footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    close() {
      this.$emit('update:visible', false);
    }
  }
};
</script>

<style scoped>
.modal-mask {
  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;
  z-index: 999;
}
.modal-container {
  background: white;
  border-radius: 4px;
  min-width: 300px;
  max-width: 80%;
}
.modal-header {
  padding: 16px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
}
.modal-body {
  padding: 16px;
}
.modal-footer {
  padding: 16px;
  border-top: 1px solid #eee;
  text-align: right;
}
.modal-close {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
}
</style>

使用模态框组件

在父组件中引入并使用模态框,通过v-model控制显示隐藏状态。可以利用插槽自定义模态框内容。

<template>
  <div>
    <button @click="showModal = true">打开模态框</button>
    <Modal v-model="showModal">
      <template #header>
        <h3>自定义标题</h3>
      </template>
      <template #body>
        <p>这里是模态框内容</p>
      </template>
      <template #footer>
        <button @click="showModal = false">关闭</button>
      </template>
    </Modal>
  </div>
</template>

<script>
import Modal from './Modal.vue';
export default {
  components: { Modal },
  data() {
    return {
      showModal: false
    };
  }
};
</script>

添加动画效果

为模态框添加淡入淡出动画,增强用户体验。使用Vue的过渡组件包裹模态框。

<template>
  <transition name="modal-fade">
    <div class="modal-mask" v-show="visible" @click.self="close">
      <!-- 其余代码不变 -->
    </div>
  </transition>
</template>

<style scoped>
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.3s;
}
.modal-fade-enter,
.modal-fade-leave-to {
  opacity: 0;
}
</style>

处理滚动锁定

模态框显示时锁定页面滚动,防止背景内容滚动。通过动态添加移除CSS类实现。

// 在Modal.vue的script部分
watch: {
  visible(newVal) {
    if (newVal) {
      document.body.classList.add('modal-open');
    } else {
      document.body.classList.remove('modal-open');
    }
  }
}
/* 全局样式 */
.modal-open {
  overflow: hidden;
}

支持ESC键关闭

监听键盘事件,按下ESC键时关闭模态框。在组件挂载和销毁时添加移除事件监听。

// 在Modal.vue的script部分
mounted() {
  document.addEventListener('keydown', this.handleKeydown);
},
beforeDestroy() {
  document.removeEventListener('keydown', this.handleKeydown);
},
methods: {
  handleKeydown(e) {
    if (e.key === 'Escape' && this.visible) {
      this.close();
    }
  }
}

响应式宽度控制

根据不同屏幕尺寸调整模态框宽度,确保在小屏幕上良好显示。

.modal-container {
  width: 50%;
}
@media (max-width: 768px) {
  .modal-container {
    width: 90%;
  }
}

vue自己实现模态框

标签: 模态vue
分享给朋友:

相关文章

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用XM…

vue实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、Go…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…