当前位置:首页 > VUE

vue 组件实现 遮罩

2026-02-17 02:09:43VUE

Vue 组件实现遮罩层

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

基础遮罩组件

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

<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>

动画遮罩

添加过渡动画效果:

<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>

全局遮罩服务

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

vue 组件实现 遮罩

// 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 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 win…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template>…

vue分类实现

vue分类实现

Vue分类实现方法 在Vue中实现分类功能通常涉及数据分组、动态渲染和交互逻辑。以下是几种常见的实现方式: 基于计算属性的分类 通过计算属性对原始数据进行分组处理,适合静态或少量数据分类: com…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…