vue3 | 自定义遮罩层组件

组件模板

ts 复制代码
<template>
  <Teleport to="body" :disabled="!appendToBody">
    <div v-bind="$attrs" v-show="modelValue" class="maskLayer-overlay" :style="{ background }">
      <div class="maskLayer-content" :style="{ marginTop: props.center ? 0 : props.top, alignItems: props.center ? 'center' : 'stretch' }">
        <slot></slot>
      </div>
    </div>
  </Teleport>
</template>

<script lang='ts' setup>
import { reactive, ref, onMounted } from 'vue';

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  center: {
    type: Boolean,
    default: false
  },
  top: {
    type: String,
    default: '30vh'
  },
  background: {
    type: String,
    default: 'rgba(0, 0, 0, 0.7)'
  },
  appendToBody: {
    type: Boolean,
    default: false
  }
})

</script>

<style lang='scss' scoped>
.maskLayer-overlay{
  position: fixed;
  margin: 0;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  z-index: 2200;
  .maskLayer-content{
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
  }
}
</style>

使用方法

html 复制代码
<maskLayer v-model="visible" center append-to-body>
  <div>内容</div>
</maskLayer>
相关推荐
程序员包打听8 小时前
从 npx 到 moonx,moonbit 的野心与展望
前端·后端
laboratory agent开发8 小时前
工具调用失败后怎么办?重试分层与降级回路的三种路线
服务器·前端·网络
IMPYLH8 小时前
HTML 的 <dialog> 元素
前端·html
AI编程实验室8 小时前
用 npm + Three.js 做一颗西瓜:把夏天的清凉感放进浏览器
前端·后端·ai编程
渣波8 小时前
基于 Milvus 构建小说知识库 RAG,实现图书智能问答(天龙八部实战)
前端·后端
橘子星8 小时前
RAG 实战:3 步将整本《天龙八部》存入向量数据库(一)
javascript·人工智能
JavaGuide8 小时前
我最推荐的 4 个 AI 编程 Skills:grill-me、research、diagnosing-bugs、code-review
前端·后端·ai编程
Moment8 小时前
2026 了,前端转 AI 全栈我是这么学的 😍😍😍
前端·后端·面试
野生风长8 小时前
c++(日期类的实现)
前端·c++
恋猫de小郭8 小时前
Android R8 为什么可以让 Kotlin 协程提速 2 倍?
android·前端·flutter