uni-popup.vue 3.6 KB

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