popup-layer.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. site:-100,
  36. timer: null,
  37. iftoggle: false,
  38. };
  39. },
  40. computed: {
  41. _translate() {
  42. const transformObj = {
  43. 'top': `transform:translateY(${-this.translateValue}%)`,
  44. 'bottom': `transform:translateY(${this.translateValue}%)`,
  45. 'left': `transform:translateX(${-this.translateValue}%)`,
  46. 'right': `transform:translateX(${this.translateValue}%)`
  47. };
  48. return transformObj[this.direction]
  49. },
  50. _location() {
  51. const positionValue = {
  52. 'top': `bottom:${this.site}%;width:100%;`,
  53. 'bottom': `top:${this.site}%;width:100%;`,
  54. 'left': `right:0px;top:0;height:100%;`,
  55. 'right': `left:0px;top:0;height:100%;`,
  56. };
  57. return positionValue[this.direction]+ this._translate;
  58. }
  59. },
  60. mounted(){
  61. if(this.showPop){
  62. // console.log(222);
  63. this.show();
  64. }
  65. },
  66. watch:{
  67. showPop(value){
  68. console.log(value)
  69. if(value){
  70. this.show();
  71. }else{
  72. this.close();
  73. }
  74. }
  75. },
  76. methods: {
  77. stopMove(event){
  78. return;
  79. },
  80. show(events) {
  81. this.ifshow = true;
  82. this.site=0;
  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. }
  113. }
  114. }
  115. </script>
  116. <style lang="scss">
  117. .popup-layer {
  118. position: fixed;
  119. z-index: 999999;
  120. // background: rgba(0, 0, 0, .3);
  121. height: 100%;
  122. width: 100%;
  123. top: 0px;
  124. left: 0px;
  125. overflow: hidden;
  126. }
  127. .popup-content {
  128. position: fixed;
  129. z-index: 1000000;
  130. background: #FFFFFF;
  131. transition: transform .2s ease;
  132. overflow: hidden;
  133. }
  134. </style>