popup-layer.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <view>
  3. <view v-show="ifshow" @tap="ableClose" @touchmove.stop.prevent class="popup-layer" >
  4. </view>
  5. <view ref="popRef" class="popup-content" @tap.stop="stopEvent" :style="_location">
  6. <slot></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. translateValue: -100, // 位移距离
  35. timer: null,
  36. iftoggle: false,
  37. };
  38. },
  39. computed: {
  40. _translate() {
  41. const transformObj = {
  42. 'top': `transform:translateY(${-this.translateValue}%)`,
  43. 'bottom': `transform:translateY(${this.translateValue}%)`,
  44. 'left': `transform:translateX(${-this.translateValue}%)`,
  45. 'right': `transform:translateX(${this.translateValue}%)`
  46. };
  47. return transformObj[this.direction]
  48. },
  49. _location() {
  50. const positionValue = {
  51. 'top': 'bottom:0px;width:100%;',
  52. 'bottom': 'top:0px;width:100%;',
  53. 'left': 'right:0px;height:100%;',
  54. 'right': 'left:0px;height:100%;',
  55. };
  56. return positionValue[this.direction] + this._translate;
  57. }
  58. },
  59. mounted(){
  60. if(this.showPop){
  61. // console.log(222);
  62. this.show();
  63. }
  64. },
  65. watch:{
  66. showPop(value){
  67. console.log(value)
  68. if(value){
  69. this.show();
  70. }else{
  71. this.close();
  72. }
  73. }
  74. },
  75. methods: {
  76. stopMove(event){
  77. console.log(11);
  78. console.log(event);
  79. return;
  80. },
  81. show(events) {
  82. this.ifshow = true;
  83. let _open = setTimeout(() => {
  84. this.translateValue = 0;
  85. _open = null;
  86. }, 100)
  87. let _toggle = setTimeout(() => {
  88. this.iftoggle = true;
  89. _toggle = null;
  90. }, 300);
  91. },
  92. close() {
  93. if (this.timer !== null || !this.iftoggle) {
  94. return;
  95. }
  96. this.translateValue = -100;
  97. this.timer = setTimeout(() => {
  98. this.ifshow = false;
  99. this.timer = null;
  100. this.iftoggle = false;
  101. this.$emit('closeCallBack', null);
  102. this.$emit('change',false)
  103. }, 300);
  104. },
  105. ableClose() {
  106. if (this.autoClose) {
  107. this.close();
  108. }
  109. },
  110. stopEvent(event) {},
  111. doSome(){
  112. console.log(111222111111111);
  113. }
  114. }
  115. }
  116. </script>
  117. <style lang="scss">
  118. .popup-layer {
  119. position: fixed;
  120. z-index: 999999;
  121. background: rgba(0, 0, 0, .3);
  122. height: 100%;
  123. width: 100%;
  124. top: 0px;
  125. left: 0px;
  126. overflow: hidden;
  127. }
  128. .popup-content {
  129. position: fixed;
  130. z-index: 1000000;
  131. background: #FFFFFF;
  132. transition: all .3s ease;
  133. overflow: hidden;
  134. // border:1px solid red;
  135. }
  136. </style>