Skip to content

Mask 遮罩层

13:10

创建一个遮罩层,用于强调特定的页面元素,并阻止用户对遮罩下层的内容进行操作,一般用于弹窗场景

平台差异说明

AppH5微信小程序支付宝小程序百度小程序头条小程序QQ小程序

基本使用

  • 通过show参数配置是否显示遮罩
  • 遮罩被点击时,会发送一个click事件,如不需要此事件,请设置mask-click-able参数为false
html
<template>
	<u-mask :show="show" @click="show = false"></u-mask>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// 定义响应式数据
const show = ref<boolean>(true)
</script>

嵌入内容

通过默认插槽可以在遮罩层上嵌入任意内容
注意:如果不想让slot插槽内容的点击事件冒泡到遮罩,请给指定元素添加上@tap.stop

html
<template>
	<u-mask :show="show" @click="show = false">
		<view class="warp">
			<view class="rect" @tap.stop></view>
		</view>
	</u-mask>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// 定义响应式数据
const show = ref<boolean>(true)
</script>

<style scoped>
.warp {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 100%;
}

.rect {
	width: 120px;
	height: 120px;
	background-color: #fff;
}
</style>

遮罩样式

  • 通过duration设置遮罩淡入淡出的时长,单位ms
  • 通过zoom设置遮罩淡入淡出时是否带有轻微的缩放效果,内部通过transform: scale(1.2, 1.2)实现
  • 通过custom-style传入一个对象,自定义样式,如"{backgroundColor: 'red', color: 'blue'}"
html
<u-mask :show="show" :duration="400" :zoom="true" :custom-style="{background: 'rgba(0, 0, 0, 0.5)'}"></u-mask>

API

Props

参数说明类型默认值可选值
show是否显示遮罩Booleanfalsetrue
z-indexz-index 层级String | Number10070-
custom-style自定义样式对象,见上方说明Object--
duration动画时长,单位毫秒String | Number300-
zoom是否使用scale对遮罩进行缩放Booleantruefalse
mask-click-able遮罩是否可点击,为false时点击不会发送click事件Booleantruefalse

Events

事件名说明回调参数
clickmask-click-abletrue时,点击遮罩发送此事件-

Slot

名称说明
default默认插槽,用于在遮罩层上方嵌入内容