当前位置:首页 > VUE

vue实现遮罩

2026-01-12 11:53:49VUE

使用 Vue 实现遮罩层

在 Vue 中实现遮罩层通常结合 CSS 和动态组件控制。以下是几种常见方法:

固定定位的遮罩层

通过 CSS 固定定位和 Vue 的 v-showv-if 控制遮罩显示:

<template>
  <div>
    <button @click="showMask = true">显示遮罩</button>
    <div class="mask" v-show="showMask" @click="showMask = false"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showMask: false
    }
  }
}
</script>

<style>
.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
}
</style>

带内容的遮罩层

在遮罩层上添加弹窗内容,并阻止事件冒泡:

<template>
  <div>
    <button @click="showModal = true">打开弹窗</button>
    <div class="mask" v-show="showModal" @click.self="showModal = false">
      <div class="modal-content">
        <h3>弹窗标题</h3>
        <p>弹窗内容...</p>
        <button @click="showModal = false">关闭</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showModal: false
    }
  }
}
</script>

<style>
.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  background: white;
  padding: 20px;
  border-radius: 5px;
  width: 300px;
}
</style>

使用过渡动画

为遮罩添加淡入淡出效果:

<template>
  <div>
    <button @click="showMask = true">显示遮罩</button>
    <transition name="fade">
      <div class="mask" v-show="showMask" @click="showMask = false"></div>
    </transition>
  </div>
</template>

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

全局遮罩组件

创建可复用的全局遮罩组件:

// Mask.vue
<template>
  <transition name="fade">
    <div class="mask" v-show="visible" @click.self="$emit('close')">
      <slot></slot>
    </div>
  </transition>
</template>

<script>
export default {
  props: {
    visible: Boolean
  }
}
</script>

<style>
.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
  display: flex;
  justify-content: center;
  align-items: center;
}

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

使用时:

vue实现遮罩

<template>
  <div>
    <button @click="showMask = true">显示遮罩</button>
    <Mask :visible="showMask" @close="showMask = false">
      <div class="modal-content">自定义内容</div>
    </Mask>
  </div>
</template>

这些方法提供了不同复杂度的遮罩实现,可根据项目需求选择合适方案。

标签: vue
分享给朋友:

相关文章

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model="…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装 scre…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue 实现原理

vue 实现原理

Vue 实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现原理主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现原理的核心要点: 响应式系统 Vu…