uni-popup.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <view v-if="showPopup" class="uni-popup" @touchmove.stop.prevent="clear">
  3. <view class="uni-popup__mask" :class="[ani+'-mask', animation ? 'mask-ani' : '']" @click="close(true)" />
  4. <view class="uni-popup__wrapper" :class="[type,ani+'-content', animation ? 'content-ani' : '']" @click="close(true)">
  5. <view class="uni-popup__wrapper-box" @click.stop="clear">
  6. <uni-status-bar />
  7. <slot />
  8. </view>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. import uniStatusBar from "../uni-status-bar/uni-status-bar.vue";
  14. export default {
  15. name: 'UniPopup',
  16. components: {
  17. uniStatusBar
  18. },
  19. props: {
  20. // 开启动画
  21. animation: {
  22. type: Boolean,
  23. default: true
  24. },
  25. // 弹出层类型,可选值,top: 顶部弹出层;bottom:底部弹出层;center:全屏弹出层
  26. type: {
  27. type: String,
  28. default: 'center'
  29. },
  30. // maskClick
  31. maskClick: {
  32. type: Boolean,
  33. default: true
  34. },
  35. show: {
  36. type: Boolean,
  37. default: true
  38. }
  39. },
  40. data() {
  41. return {
  42. ani: '',
  43. showPopup: false
  44. }
  45. },
  46. watch: {
  47. show(newValue) {
  48. if (newValue) {
  49. this.open()
  50. } else {
  51. this.close()
  52. }
  53. }
  54. },
  55. created() {
  56. // this.open()
  57. },
  58. methods: {
  59. clear() {},
  60. open() {
  61. this.$emit('change', {
  62. show: true
  63. })
  64. this.showPopup = true
  65. this.$nextTick(() => {
  66. setTimeout(() => {
  67. this.ani = 'uni-' + this.type
  68. }, 100)
  69. })
  70. },
  71. close(type) {
  72. if (!this.maskClick && type) return
  73. this.$emit('change', {
  74. show: false
  75. })
  76. this.ani = ''
  77. this.$nextTick(() => {
  78. setTimeout(() => {
  79. this.showPopup = false
  80. }, 300)
  81. })
  82. }
  83. }
  84. }
  85. </script>
  86. <style scoped>
  87. .uni-popup {
  88. position: fixed;
  89. /* #ifdef H5 */
  90. top: var(--window-top);
  91. /* #endif */
  92. /* #ifndef H5 */
  93. top: 0;
  94. /* #endif */
  95. bottom: 0;
  96. left: 0;
  97. right: 0;
  98. overflow: hidden;
  99. }
  100. .uni-popup__mask {
  101. position: absolute;
  102. top: 0;
  103. bottom: 0;
  104. left: 0;
  105. right: 0;
  106. background-color: rgba(0, 0, 0, 0.4);
  107. opacity: 0;
  108. }
  109. .mask-ani {
  110. transition-property: opacity;
  111. transition-duration: 0.2s;
  112. }
  113. .uni-top-mask {
  114. opacity: 1;
  115. }
  116. .uni-bottom-mask {
  117. opacity: 1;
  118. }
  119. .uni-center-mask {
  120. opacity: 1;
  121. }
  122. .uni-popup__wrapper {
  123. /* #ifndef APP-NVUE */
  124. display: block;
  125. /* #endif */
  126. position: absolute;
  127. }
  128. .top {
  129. top: 0;
  130. left: 0;
  131. right: 0;
  132. transform: translateY(-500px);
  133. }
  134. .bottom {
  135. bottom: 0;
  136. left: 0;
  137. right: 0;
  138. transform: translateY(500px);
  139. }
  140. .center {
  141. /* #ifndef APP-NVUE */
  142. display: flex;
  143. flex-direction: column;
  144. /* #endif */
  145. bottom: 0;
  146. left: 0;
  147. right: 0;
  148. top: 0;
  149. justify-content: center;
  150. align-items: center;
  151. transform: scale(1.2);
  152. opacity: 0;
  153. }
  154. .uni-popup__wrapper-box {
  155. /* #ifndef APP-NVUE */
  156. display: block;
  157. /* #endif */
  158. position: relative;
  159. }
  160. .content-ani {
  161. /* transition: transform 0.3s;
  162. */
  163. transition-property: transform, opacity;
  164. transition-duration: 0.2s;
  165. }
  166. .uni-top-content {
  167. transform: translateY(0);
  168. }
  169. .uni-bottom-content {
  170. transform: translateY(0);
  171. }
  172. .uni-center-content {
  173. transform: scale(1);
  174. opacity: 1;
  175. }
  176. </style>