u-overlay.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <u-transition
  3. :show="show"
  4. custom-class="u-overlay"
  5. :duration="duration"
  6. :custom-style="overlayStyle"
  7. @tap.stop="clickHandler"
  8. @touchmove.stop.prevent="noop"
  9. >
  10. <slot />
  11. </u-transition>
  12. </template>
  13. <script>
  14. import props from './props.js';
  15. import mixin from '../../libs/mixin/mixin'
  16. import mpMixin from '../../libs/mixin/mpMixin';
  17. /**
  18. * overlay 遮罩
  19. * @description 创建一个遮罩层,用于强调特定的页面元素,并阻止用户对遮罩下层的内容进行操作,一般用于弹窗场景
  20. * @tutorial https://uview.d3u.cn/components/overlay.html
  21. * @property {Boolean} show 是否显示遮罩(默认 false )
  22. * @property {String | Number} zIndex zIndex 层级(默认 10070 )
  23. * @property {String | Number} duration 动画时长,单位毫秒(默认 300 )
  24. * @property {String | Number} opacity 不透明度值,当做rgba的第四个参数 (默认 0.5 )
  25. * @property {Object} customStyle 定义需要用到的外部样式
  26. * @event {Function} click 点击遮罩发送事件
  27. * @example <u-overlay :show="show" @click="show = false"></u-overlay>
  28. */
  29. export default {
  30. name: "u-overlay",
  31. mixins: [mpMixin, mixin, props],
  32. computed: {
  33. overlayStyle() {
  34. const style = {
  35. position: 'fixed',
  36. top: 0,
  37. left: 0,
  38. right: 0,
  39. zIndex: this.zIndex,
  40. bottom: 0,
  41. backgroundColor: `rgba(0, 0, 0, ${this.opacity})`
  42. }
  43. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle))
  44. }
  45. },
  46. // #ifdef VUE3
  47. emits: ["click"],
  48. // #endif
  49. methods: {
  50. clickHandler() {
  51. this.$emit('click')
  52. }
  53. }
  54. }
  55. </script>
  56. <style lang="scss" scoped>
  57. @import "../../libs/css/components.scss";
  58. $u-overlay-top:0 !default;
  59. $u-overlay-left:0 !default;
  60. $u-overlay-bottom:0 !default;
  61. $u-overlay-right:0 !default;
  62. $u-overlay-width:100% !default;
  63. $u-overlay-height:100% !default;
  64. $u-overlay-background-color:rgba(0, 0, 0, .7) !default;
  65. .u-overlay {
  66. position: fixed;
  67. top:$u-overlay-top;
  68. left:$u-overlay-left;
  69. right:$u-overlay-right;
  70. bottom:$u-overlay-bottom;
  71. width: $u-overlay-width;
  72. height:$u-overlay-height;
  73. background-color:$u-overlay-background-color;
  74. }
  75. </style>