blob: 0e76d8bec60fca93631bff34417adbd5cb0fa81c [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <view class="u-mask" hover-stop-propagation :style="[maskStyle, zoomStyle]" @tap="click" @touchmove.stop.prevent="() => {}" :class="{
3 'u-mask-zoom': zoom,
4 'u-mask-show': show
5 }">
6 <slot />
7 </view>
8</template>
9
10<script>
11 /**
12 * mask 遮罩
13 * @description 创建一个遮罩层,用于强调特定的页面元素,并阻止用户对遮罩下层的内容进行操作,一般用于弹窗场景
14 * @tutorial https://www.uviewui.com/components/mask.html
15 * @property {Boolean} show 是否显示遮罩(默认false)
16 * @property {String Number} z-index z-index 层级(默认1070)
17 * @property {Object} custom-style 自定义样式对象,见上方说明
18 * @property {String Number} duration 动画时长,单位毫秒(默认300)
19 * @property {Boolean} zoom 是否使用scale对这招进行缩放(默认true)
20 * @property {Boolean} mask-click-able 遮罩是否可点击,为false时点击不会发送click事件(默认true)
21 * @event {Function} click mask-click-able为true时,点击遮罩发送此事件
22 * @example <u-mask :show="show" @click="show = false"></u-mask>
23 */
24 export default {
25 name: "u-mask",
26 props: {
27 // 是否显示遮罩
28 show: {
29 type: Boolean,
30 default: false
31 },
32 // 层级z-index
33 zIndex: {
34 type: [Number, String],
35 default: ''
36 },
37 // 用户自定义样式
38 customStyle: {
39 type: Object,
40 default () {
41 return {}
42 }
43 },
44 // 遮罩的动画样式, 是否使用使用zoom进行scale进行缩放
45 zoom: {
46 type: Boolean,
47 default: true
48 },
49 // 遮罩的过渡时间,单位为ms
50 duration: {
51 type: [Number, String],
52 default: 300
53 },
54 // 是否可以通过点击遮罩进行关闭
55 maskClickAble: {
56 type: Boolean,
57 default: true
58 }
59 },
60 data() {
61 return {
62 zoomStyle: {
63 transform: ''
64 },
65 scale: 'scale(1.2, 1.2)'
66 }
67 },
68 watch: {
69 show(n) {
70 if(n && this.zoom) {
71 // 当展示遮罩的时候,设置scale为1,达到缩小(原来为1.2)的效果
72 this.zoomStyle.transform = 'scale(1, 1)';
73 } else if(!n && this.zoom) {
74 // 当隐藏遮罩的时候,设置scale为1.2,达到放大(因为显示遮罩时已重置为1)的效果
75 this.zoomStyle.transform = this.scale;
76 }
77 }
78 },
79 computed: {
80 maskStyle() {
81 let style = {};
82 style.backgroundColor = "rgba(0, 0, 0, 0.6)";
83 if(this.show) style.zIndex = this.zIndex ? this.zIndex : this.$u.zIndex.mask;
84 else style.zIndex = -1;
85 style.transition = `all ${this.duration / 1000}s ease-in-out`;
86 // 判断用户传递的对象是否为空,不为空就进行合并
87 if (Object.keys(this.customStyle).length) style = {
88 ...style,
89 ...this.customStyle
90 };
91 return style;
92 }
93 },
94 methods: {
95 click() {
96 if (!this.maskClickAble) return;
97 this.$emit('click');
98 }
99 }
100 }
101</script>
102
103<style lang="scss" scoped>
104 @import "../../libs/css/style.components.scss";
105
106 .u-mask {
107 position: fixed;
108 top: 0;
109 left: 0;
110 right: 0;
111 bottom: 0;
112 opacity: 0;
113 transition: transform 0.3s;
114 }
115
116 .u-mask-show {
117 opacity: 1;
118 }
119
120 .u-mask-zoom {
121 transform: scale(1.2, 1.2);
122 }
123</style>