You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
3.5 KiB

6 months ago
  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: true
  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: 998;
  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. }
  159. &.uni-custom {
  160. & .uni-popup__wrapper-box {
  161. padding: 30upx;
  162. background: #fff;
  163. }
  164. &.center {
  165. & .uni-popup__wrapper-box {
  166. position: relative;
  167. max-width: 80%;
  168. max-height: 80%;
  169. overflow-y: scroll;
  170. }
  171. }
  172. &.top,
  173. &.bottom {
  174. & .uni-popup__wrapper-box {
  175. width: 100%;
  176. max-height: 500px;
  177. overflow-y: scroll;
  178. }
  179. }
  180. }
  181. &.uni-top,
  182. &.uni-bottom {
  183. transform: translateY(0);
  184. }
  185. &.uni-center {
  186. transform: scale(1);
  187. opacity: 1;
  188. }
  189. }
  190. }
  191. </style>