my-popup.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view v-if="showPopup" class="uni-popup" id="uni-popup---id">
  3. <view :class="[ani, animation ? 'ani' : '', !custom ? 'uni-custom' : '']" class="uni-popup__mask" @click="close(true)" />
  4. <view :class="['type-'+type, ani, animation ? 'ani' : '', !custom ? 'uni-custom' : '']" class="uni-popup__wrapper" @click="close(true)" >
  5. <view class="uni-popup__wrapper-box" @click.stop="clear" :style="{width:width,height:height}">
  6. <slot />
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'my-popup',
  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: false
  38. },
  39. height:{
  40. type: String,
  41. default: 'inherit'
  42. },
  43. width:{
  44. type: String,
  45. default: 'inherit'
  46. },
  47. },
  48. data() {
  49. return {
  50. ani: '',
  51. showPopup: false
  52. }
  53. },
  54. watch: {
  55. show(newValue) {
  56. if (newValue) {
  57. this.open()
  58. } else {
  59. this.close()
  60. }
  61. }
  62. },
  63. created() {},
  64. mounted: function() {
  65. },
  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.$emit('hidePopup')
  85. this.ani = ''
  86. this.$nextTick(() => {
  87. setTimeout(() => {
  88. this.showPopup = false
  89. }, 300)
  90. })
  91. }
  92. }
  93. }
  94. </script>
  95. <style>
  96. @charset "UTF-8";
  97. .uni-popup {
  98. position: fixed;
  99. top: 0;
  100. top: 0;
  101. bottom: 0;
  102. left: 0;
  103. right: 0;
  104. z-index: 998;
  105. overflow: hidden
  106. }
  107. .uni-popup__mask {
  108. position: absolute;
  109. top: 0;
  110. bottom: 0;
  111. left: 0;
  112. right: 0;
  113. z-index: 998;
  114. background: rgba(0, 0, 0, .4);
  115. opacity: 0
  116. }
  117. .uni-popup__mask.ani {
  118. transition: all .3s
  119. }
  120. .uni-popup__mask.uni-bottom,
  121. .uni-popup__mask.uni-center,
  122. .uni-popup__mask.uni-top {
  123. opacity: 1
  124. }
  125. .uni-popup__wrapper {
  126. position: absolute;
  127. z-index: 999;
  128. box-sizing: border-box
  129. }
  130. .uni-popup__wrapper.ani {
  131. transition: all .3s
  132. }
  133. .uni-popup__wrapper.type-top {
  134. top: 0;
  135. left: 0;
  136. width: 100%;
  137. transform: translateY(-100%)
  138. }
  139. .uni-popup__wrapper.type-bottom {
  140. bottom: 0;
  141. left: 0;
  142. width: 100%;
  143. transform: translateY(100%)
  144. }
  145. .uni-popup__wrapper.type-center {
  146. bottom: 0;
  147. top: 0;
  148. left: 0;
  149. right: 0;
  150. width: 100%;
  151. height: inherit;
  152. display: flex;
  153. justify-content: center;
  154. align-items: center;
  155. transform: scale(1.2);
  156. opacity: 0;
  157. }
  158. .uni-popup__wrapper-box {
  159. position: relative;
  160. box-sizing: border-box
  161. }
  162. .uni-popup__wrapper.uni-custom .uni-popup__wrapper-box {
  163. background: #fff;
  164. }
  165. .uni-popup__wrapper.uni-custom.type-center .uni-popup__wrapper-box {
  166. position: relative;
  167. border-radius: 15rpx;
  168. max-width: 100%;
  169. max-height: 100%;
  170. overflow-y: scroll
  171. }
  172. .uni-popup__wrapper.uni-custom.type-bottom .uni-popup__wrapper-box,
  173. .uni-popup__wrapper.uni-custom.type-top .uni-popup__wrapper-box {
  174. width: 100%;
  175. overflow-y: scroll
  176. }
  177. .uni-popup__wrapper.uni-bottom,
  178. .uni-popup__wrapper.uni-top {
  179. transform: translateY(0)
  180. }
  181. .uni-popup__wrapper.uni-center {
  182. transform: scale(1);
  183. opacity: 1
  184. }
  185. </style>