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>
相关推荐
GIS之路12 分钟前
ArcGIS Pro 中的 Notebooks 入门
前端
IT_陈寒1 小时前
React状态管理终极对决:Redux vs Context API谁更胜一筹?
前端·人工智能·后端
lemon_yyds2 小时前
《vue 2 升级vue3 父组件 子组件 传值: value 和 v-model
vue.js
Kagol3 小时前
TinyVue 支持 Skills 啦!现在你可以让 AI 使用 TinyVue 组件搭建项目
前端·agent·ai编程
柳杉3 小时前
从零打造 AI 全球趋势监测大屏
前端·javascript·aigc
simple_lau3 小时前
Cursor配置MasterGo MCP:一键读取设计稿生成高还原度前端代码
前端·javascript·vue.js
睡不着先生3 小时前
如何设计一个真正可扩展的表单生成器?
前端·javascript·vue.js
天蓝色的鱼鱼3 小时前
模块化与组件化:90%的前端开发者都没搞懂的本质区别
前端·架构·代码规范
明君879973 小时前
Flutter 如何给图片添加多行文字水印
前端·flutter
进击的尘埃3 小时前
AI 代码审查工具链搭建:用 AST 解析 + LLM 实现自动化 Code Review 的前端工程方案
javascript