uni-popup.vue 3.7 KB

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