popup-layer.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <scroll-view scroll-y v-show="ifshow" @tap="ableClose" @touchmove.stop.prevent class="popup-layer">
  3. <view ref="popRef" class="popup-content" @tap.stop="stopEvent" :style="_location">
  4. <slot></slot>
  5. </view>
  6. </scroll-view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'popup-layer',
  11. props: {
  12. direction: {
  13. type: String,
  14. default: 'top', // 方向 top,bottom,left,right
  15. },
  16. autoClose: {
  17. type: Boolean,
  18. default: true,
  19. },
  20. isTransNav: {
  21. type: Boolean,
  22. default: false
  23. },
  24. navHeight: {
  25. type: Number,
  26. default: 0
  27. }
  28. },
  29. data() {
  30. return {
  31. ifshow: false, // 是否展示,
  32. translateValue: -100, // 位移距离
  33. timer: null,
  34. iftoggle: false,
  35. };
  36. },
  37. computed: {
  38. _translate() {
  39. if (this.isTransNav) {
  40. const transformObj = {
  41. 'top': `transform:translateY(${-this.translateValue}%)`,
  42. 'bottom': `transform:translateY(calc(${this.translateValue}% + ${this.navHeight}px))`,
  43. 'left': `transform:translateX(${-this.translateValue}%)`,
  44. 'right': `transform:translateX(${this.translateValue}%)`
  45. };
  46. return transformObj[this.direction]
  47. } else {
  48. const transformObj = {
  49. 'top': `transform:translateY(${-this.translateValue}%)`,
  50. 'bottom': `transform:translateY(${this.translateValue}%)`,
  51. 'left': `transform:translateX(${-this.translateValue}%)`,
  52. 'right': `transform:translateX(${this.translateValue}%)`
  53. };
  54. return transformObj[this.direction]
  55. }
  56. },
  57. _location() {
  58. const positionValue = {
  59. 'top': 'bottom:0px;width:100%;',
  60. 'bottom': 'top:0px;width:100%;',
  61. 'left': 'right:0px;height:100%;',
  62. 'right': 'left:0px;height:100%;',
  63. };
  64. return positionValue[this.direction] + this._translate;
  65. }
  66. },
  67. methods: {
  68. show() {
  69. let _this = this;
  70. this.ifshow = true;
  71. let _open = setTimeout(() => {
  72. this.translateValue = 0;
  73. _open = null;
  74. }, 100)
  75. let _toggle = setTimeout(() => {
  76. this.iftoggle = true;
  77. _toggle = null;
  78. }, 300);
  79. },
  80. close() {
  81. if (this.timer !== null || !this.iftoggle) {
  82. return;
  83. }
  84. this.translateValue = -100 - this.navHeight;
  85. this.timer = setTimeout(() => {
  86. this.ifshow = false;
  87. this.timer = null;
  88. this.iftoggle = false;
  89. }, 300);
  90. this.$emit("close")
  91. },
  92. ableClose() {
  93. if (this.autoClose) {
  94. this.close();
  95. }
  96. },
  97. stopEvent(event) {},
  98. }
  99. }
  100. </script>
  101. <style>
  102. .popup-layer {
  103. position: absolute;
  104. z-index: 999999;
  105. background: rgba(0, 0, 0, .3);
  106. /* height: calc(100% - 50px); */
  107. height: 1000px;
  108. width: 100%;
  109. left: 0px;
  110. overflow: hidden;
  111. }
  112. .popup-content {
  113. position: absolute;
  114. z-index: 1000000;
  115. background: #FFFFFF;
  116. transition: all .3s ease;
  117. }
  118. </style>