u-overlay.vue 2.1 KB

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