u-mask.vue 2.6 KB

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