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>
相关推荐
ct9788 小时前
组件间的通信
前端·javascript·vue.js
左手吻左脸。8 小时前
Vue 全栈面试题大全(2026 最新版最详细)
前端·javascript·vue.js
Aphasia3118 小时前
手写KeepAlive组件
前端·react.js·面试
两个西柚呀9 小时前
js中的同步和异步,三种处理异步任务的方式
前端·javascript
pe7er9 小时前
软件设计不要“既要又要”
前端·后端·架构
kyriewen9 小时前
从Webpack到Vite:我们迁移了一个10万行代码的项目,总结了这7个坑
前端·webpack·vite
IT_陈寒9 小时前
Java Stream并行流的坑:我花了3小时才找到的线程安全问题
前端·人工智能·后端
小新1109 小时前
最简单但完整的 Vue 响应式示例(一个简单的计数器按钮)
前端·javascript·vue.js
川冰ICE9 小时前
JavaScript进阶④|Symbol与元编程,对象的隐藏身份
开发语言·javascript·ecmascript
水煮白菜王10 小时前
开源 AI 桌宠 Clawd on Desk:让 Claude Code 的状态从终端‘蹦‘到桌面
javascript·人工智能·开源