uni-popup.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view v-if="showPopup" class="uni-popup">
  3. <view :class="[ani, animation ? 'ani' : '', !custom ? 'uni-custom' : '']" class="uni-popup__mask" @click="close(true)" />
  4. <view :class="[type, ani, animation ? 'ani' : '', !custom ? 'uni-custom' : '']" class="uni-popup__wrapper" @click="close(true)">
  5. <view class="uni-popup__wrapper-box" @click.stop="clear">
  6. <slot />
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'UniPopup',
  14. props: {
  15. // 开启动画
  16. animation: {
  17. type: Boolean,
  18. default: true
  19. },
  20. // 弹出层类型,可选值,top: 顶部弹出层;bottom:底部弹出层;center:全屏弹出层
  21. type: {
  22. type: String,
  23. default: 'center'
  24. },
  25. // 是否开启自定义
  26. custom: {
  27. type: Boolean,
  28. default: false
  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. methods: {
  57. clear() {},
  58. open() {
  59. this.$emit('change', {
  60. show: true
  61. })
  62. this.showPopup = true
  63. this.$nextTick(() => {
  64. setTimeout(() => {
  65. this.ani = 'uni-' + this.type
  66. }, 30)
  67. })
  68. },
  69. close(type) {
  70. if (!this.maskClick && type) return
  71. this.$emit('change', {
  72. show: false
  73. })
  74. this.ani = ''
  75. this.$nextTick(() => {
  76. setTimeout(() => {
  77. this.showPopup = false
  78. }, 300)
  79. })
  80. }
  81. }
  82. }
  83. </script>
  84. <style>
  85. @charset "UTF-8";
  86. .uni-popup {
  87. position: fixed;
  88. top: 0;
  89. top: 0;
  90. bottom: 0;
  91. left: 0;
  92. right: 0;
  93. z-index: 9998;
  94. overflow: hidden
  95. }
  96. .uni-popup__mask {
  97. position: absolute;
  98. top: 0;
  99. bottom: 0;
  100. left: 0;
  101. right: 0;
  102. z-index: 998;
  103. background: rgba(0, 0, 0, .4);
  104. opacity: 0
  105. }
  106. .uni-popup__mask.ani {
  107. transition: all .3s
  108. }
  109. .uni-popup__mask.uni-bottom,
  110. .uni-popup__mask.uni-center,
  111. .uni-popup__mask.uni-top {
  112. opacity: 1
  113. }
  114. .uni-popup__wrapper {
  115. position: absolute;
  116. z-index: 999;
  117. box-sizing: border-box
  118. }
  119. .uni-popup__wrapper.ani {
  120. transition: all .3s
  121. }
  122. .uni-popup__wrapper.top {
  123. top: 0;
  124. left: 0;
  125. width: 100%;
  126. transform: translateY(-100%)
  127. }
  128. .uni-popup__wrapper.bottom {
  129. bottom: 0;
  130. left: 0;
  131. width: 100%;
  132. transform: translateY(100%)
  133. }
  134. .uni-popup__wrapper.center {
  135. width: 100%;
  136. height: 100%;
  137. display: flex;
  138. justify-content: center;
  139. align-items: center;
  140. transform: scale(1.2);
  141. opacity: 0
  142. }
  143. .uni-popup__wrapper-box {
  144. position: relative;
  145. box-sizing: border-box
  146. }
  147. .uni-popup__wrapper.uni-custom .uni-popup__wrapper-box {
  148. /* padding: 30upx; */
  149. background: #fff
  150. }
  151. .uni-popup__wrapper.uni-custom.center .uni-popup__wrapper-box {
  152. position: relative;
  153. max-width: 80%;
  154. max-height: 80%;
  155. overflow-y: scroll
  156. }
  157. .uni-popup__wrapper.uni-custom.bottom .uni-popup__wrapper-box,
  158. .uni-popup__wrapper.uni-custom.top .uni-popup__wrapper-box {
  159. width: 100%;
  160. max-height: 500px;
  161. overflow-y: scroll
  162. }
  163. .uni-popup__wrapper.uni-bottom,
  164. .uni-popup__wrapper.uni-top {
  165. transform: translateY(0)
  166. }
  167. .uni-popup__wrapper.uni-center {
  168. transform: scale(1);
  169. opacity: 1
  170. }
  171. </style>