u-mask.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. export default {
  8. props: {
  9. // 是否显示遮罩
  10. show: {
  11. type: Boolean,
  12. default: false
  13. },
  14. // 层级z-index
  15. zIndex: {
  16. type: [Number, String],
  17. default: 1001
  18. },
  19. // 用户自定义样式
  20. customStyle: {
  21. type: Object,
  22. default () {
  23. return {}
  24. }
  25. },
  26. // 遮罩的动画样式, 是否使用使用zoom进行scale进行缩放
  27. zoom: {
  28. type: Boolean,
  29. default: true
  30. },
  31. // 遮罩的过渡时间,单位为ms
  32. duration: {
  33. type: [Number, String],
  34. default: 300
  35. },
  36. // 是否可以通过点击遮罩进行关闭
  37. maskClickAble: {
  38. type: Boolean,
  39. default: true
  40. }
  41. },
  42. computed: {
  43. maskStyle() {
  44. let style = {};
  45. style.backgroundColor = "rgba(0, 0, 0, 0.6)";
  46. style.zIndex = this.zIndex;
  47. style.transition = `all ${this.duration / 1000}s ease-in-out`;
  48. // 缩放
  49. if (this.zoom == true) style.transform = 'scale(1.2, 1.2)';
  50. // 判断用户传递的对象是否为空
  51. if (Object.keys(this.customStyle).length) style = { ...style,
  52. ...this.customStyle
  53. };
  54. // 合并自定义的样式
  55. //Object.assign(style, customStyle);
  56. return style;
  57. }
  58. },
  59. methods: {
  60. click() {
  61. if(!this.maskClickAble) return ;
  62. this.$emit('click');
  63. }
  64. }
  65. }
  66. </script>
  67. <style lang="scss" scoped>
  68. .u-mask {
  69. position: fixed;
  70. top: 0;
  71. left: 0;
  72. right: 0;
  73. bottom: 0;
  74. opacity: 0;
  75. visibility: hidden;
  76. }
  77. .u-mask-show {
  78. opacity: 1;
  79. visibility: visible;
  80. transform: scale(1);
  81. }
  82. </style>