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>
相关推荐
做一位快乐的码农2 分钟前
基于springboot的理商管理平台设计与实现、java/vue/mvc
java·vue.js·spring boot
盛夏绽放6 小时前
jQuery 知识点复习总览
前端·javascript·jquery
胡gh8 小时前
依旧性能优化,如何在浅比较上做文章,memo 满天飞,谁在裸奔?
前端·react.js·面试
大怪v9 小时前
超赞👍!优秀前端佬的电子布洛芬技术网站!
前端·javascript·vue.js
胡gh9 小时前
你一般用哪些状态管理库?别担心,Zustand和Redux就能说个10分钟
前端·面试·node.js
项目題供诗9 小时前
React学习(十二)
javascript·学习·react.js
无羡仙9 小时前
Webpack 背后做了什么?
javascript·webpack
老华带你飞10 小时前
校园交友|基于SprinBoot+vue的校园交友网站(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·校园交友网站
roamingcode10 小时前
Claude Code NPM 包发布命令
前端·npm·node.js·claude·自定义指令·claude code
码哥DFS10 小时前
NPM模块化总结
前端·javascript