popup-layer.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view>
  3. <view v-show="ifshow" @tap="ableClose" @touchmove.stop.prevent class="popup-layer" >
  4. </view>
  5. <view class="popup-content" @tap.stop="stopEvent" :style="_location">
  6. <slot name="content"></slot>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'popup-layer',
  13. model: {
  14. prop: "showPop",
  15. event: "change"
  16. },
  17. props: {
  18. showPop:{
  19. type:Boolean,
  20. default:false,
  21. },
  22. direction: {
  23. type: String,
  24. default: 'top', // 方向 top,bottom,left,right
  25. },
  26. autoClose: {
  27. type: Boolean,
  28. default: true,
  29. }
  30. },
  31. data() {
  32. return {
  33. ifshow: false, // 是否展示,
  34. //#ifdef H5
  35. translateValue: -150, // 位移距离
  36. //#endif
  37. //#ifndef H5
  38. translateValue: -100, // 位移距离
  39. //#endif
  40. timer: null,
  41. iftoggle: false,
  42. };
  43. },
  44. computed: {
  45. _translate() {
  46. const transformObj = {
  47. 'top': `transform:translateY(${-this.translateValue}%)`,
  48. 'bottom': `transform:translateY(${this.translateValue}%)`,
  49. 'left': `transform:translateX(${-this.translateValue}%)`,
  50. 'right': `transform:translateX(${this.translateValue}%)`
  51. };
  52. return transformObj[this.direction]
  53. },
  54. _location() {
  55. const positionValue = {
  56. //#ifndef H5
  57. 'top': 'bottom:0px;width:100%;',
  58. //#endif
  59. //#ifdef H5
  60. 'top': 'bottom:50px;width:100%;',
  61. //#endif
  62. 'bottom': 'top:0px;width:100%;',
  63. 'left': 'right:0px;height:100%;',
  64. 'right': 'left:0px;height:100%;',
  65. };
  66. return positionValue[this.direction] + this._translate;
  67. }
  68. },
  69. mounted(){
  70. if(this.showPop){
  71. // console.log(222);
  72. this.show();
  73. }
  74. },
  75. watch:{
  76. showPop(value){
  77. console.log(value)
  78. if(value){
  79. this.show();
  80. }else{
  81. this.close();
  82. }
  83. }
  84. },
  85. methods: {
  86. stopMove(event){
  87. console.log(11);
  88. console.log(event);
  89. return;
  90. },
  91. show(events) {
  92. this.ifshow = true;
  93. let _open = setTimeout(() => {
  94. this.translateValue = 0;
  95. _open = null;
  96. }, 100)
  97. let _toggle = setTimeout(() => {
  98. this.iftoggle = true;
  99. _toggle = null;
  100. }, 300);
  101. },
  102. close() {
  103. if (this.timer !== null || !this.iftoggle) {
  104. return;
  105. }
  106. this.translateValue = -100;
  107. //#ifdef H5
  108. this.translateValue = -150
  109. //#endif
  110. this.timer = setTimeout(() => {
  111. this.ifshow = false;
  112. this.timer = null;
  113. this.iftoggle = false;
  114. this.$emit('closeCallBack', null);
  115. this.$emit('change',false)
  116. }, 300);
  117. },
  118. ableClose() {
  119. if (this.autoClose) {
  120. this.close();
  121. }
  122. },
  123. stopEvent(event) {},
  124. doSome(){
  125. console.log(111222111111111);
  126. }
  127. }
  128. }
  129. </script>
  130. <style lang="scss">
  131. .popup-layer {
  132. position: fixed;
  133. z-index: 9990;
  134. background: rgba(0, 0, 0, .3);
  135. height: 100%;
  136. width: 100%;
  137. top: 0px;
  138. left: 0px;
  139. overflow: hidden;
  140. }
  141. .popup-content {
  142. position: fixed;
  143. z-index: 9991;
  144. background: #FFFFFF;
  145. transition: all .3s ease;
  146. overflow: hidden;
  147. box-sizing: border-box;
  148. // border:1px solid red;
  149. }
  150. </style>