当前位置:首页 > VUE

vue 组件实现 遮罩

2026-02-17 02:09:43VUE

Vue 组件实现遮罩层

遮罩层(Overlay)是常见的 UI 组件,用于在页面上覆盖半透明背景,通常配合弹窗、菜单等使用。以下是几种实现方式:

基础遮罩组件

通过固定定位和半透明背景实现基础遮罩:

vue 组件实现 遮罩

<template>
  <div v-if="visible" class="overlay" @click.self="handleClose">
    <slot></slot>
  </div>
</template>

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

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

动画遮罩

添加过渡动画效果:

vue 组件实现 遮罩

<template>
  <transition name="fade">
    <div v-if="visible" class="overlay" @click.self="handleClose">
      <slot></slot>
    </div>
  </transition>
</template>

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

可配置遮罩

扩展组件支持更多配置选项:

<script>
export default {
  props: {
    visible: Boolean,
    clickClose: {
      type: Boolean,
      default: true
    },
    backgroundColor: {
      type: String,
      default: 'rgba(0, 0, 0, 0.5)'
    },
    zIndex: {
      type: Number,
      default: 999
    }
  },
  methods: {
    handleClose() {
      if (this.clickClose) {
        this.$emit('close')
      }
    }
  }
}
</script>

<style scoped>
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: v-bind(backgroundColor);
  z-index: v-bind(zIndex);
}
</style>

全局遮罩服务

创建可编程控制的遮罩服务:

// overlay.service.js
import Vue from 'vue'

const OverlayComponent = Vue.extend({
  template: `
    <div class="overlay" :style="{ zIndex }">
      <slot></slot>
    </div>
  `,
  props: {
    zIndex: {
      type: Number,
      default: 999
    }
  }
})

export default {
  show(content, options = {}) {
    const instance = new OverlayComponent({
      propsData: options
    })
    if (content) {
      instance.$slots.default = [content]
    }
    instance.$mount()
    document.body.appendChild(instance.$el)
    return instance
  }
}

使用示例

<template>
  <button @click="showOverlay">显示遮罩</button>
  <Overlay :visible="isVisible" @close="isVisible = false">
    <div class="modal-content">这里是弹窗内容</div>
  </Overlay>
</template>

<script>
import Overlay from './Overlay.vue'

export default {
  components: { Overlay },
  data() {
    return {
      isVisible: false
    }
  },
  methods: {
    showOverlay() {
      this.isVisible = true
    }
  }
}
</script>

这些实现方式可根据项目需求选择或组合使用,基础组件适合简单场景,全局服务适合复杂应用。

标签: 组件vue
分享给朋友:

相关文章

vue页面分离的实现

vue页面分离的实现

Vue页面分离的实现方法 将Vue页面分离为多个组件或模块,有助于提升代码可维护性和复用性。以下是几种常见的实现方式: 组件化拆分 通过将页面拆分为多个子组件,每个组件负责特定功能或UI部分。使用…

vue实现下载暂停

vue实现下载暂停

Vue实现下载暂停功能 在Vue中实现下载暂停功能,通常需要结合XMLHttpRequest或Fetch API的AbortController来控制请求中断。以下是具体实现方法: 使用XMLHtt…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在…

vue 实现回复

vue 实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及表单提交、数据绑定和列表渲染。以下是实现步骤和代码示例: 数据绑定与表单 <template> <div>…

vue 实现拖拉

vue 实现拖拉

Vue 实现拖拽功能的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 cla…