uni-popup.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view v-if="showPopup" class="uni-popup" @touchmove.stop.prevent="clear">
  3. <uni-transition :mode-class="['fade']" :styles="maskClass"
  4. :duration="duration" :show="showTrans" @click="onTap" />
  5. <uni-transition :mode-class="ani" :styles="transClass"
  6. :duration="duration" :show="showTrans">
  7. <view class="uni-popup__wrapper-box">
  8. <slot />
  9. </view>
  10. </uni-transition>
  11. </view>
  12. </template>
  13. <script>
  14. import uniTransition from './uni-transition.vue'
  15. /**
  16. * PopUp 弹出层
  17. * @description 弹出层组件,为了解决遮罩弹层的问题
  18. * @tutorial https://ext.dcloud.net.cn/plugin?id=329
  19. * @property {String} type = [top|center|bottom] 弹出方式
  20. * @value top 顶部弹出
  21. * @value left 左边弹出
  22. * @value center 中间弹出
  23. * @value bottom 底部弹出
  24. * @property {Boolean} animation = [ture|false] 是否开启动画
  25. * @property {Boolean} maskClick = [ture|false] 蒙版点击是否关闭弹窗
  26. * @event {Function} change 打开关闭弹窗触发,e={show: false}
  27. */
  28. export default {
  29. name: 'UniPopup',
  30. components: {
  31. uniTransition
  32. },
  33. props: {
  34. // 开启动画
  35. animation: {
  36. type: Boolean,
  37. default: true
  38. },
  39. // 弹出层类型,可选值,top: 顶部弹出层;bottom:底部弹出层;center:全屏弹出层
  40. type: {
  41. type: String,
  42. default: 'center'
  43. },
  44. // maskClick
  45. maskClick: {
  46. type: Boolean,
  47. default: true
  48. }
  49. },
  50. data() {
  51. return {
  52. duration: 300,
  53. ani: [],
  54. showPopup: false,
  55. showTrans: false,
  56. maskClass: {
  57. 'position': 'fixed',
  58. 'bottom': 0,
  59. 'top': 0,
  60. 'left': 0,
  61. 'right': 0,
  62. 'backgroundColor': 'rgba(0, 0, 0, 0.4)'
  63. },
  64. transClass: {
  65. 'position': 'fixed',
  66. 'left': 0,
  67. 'right': 0,
  68. }
  69. }
  70. },
  71. watch: {
  72. type: {
  73. handler: function(newVal) {
  74. switch (this.type) {
  75. case 'top':
  76. this.ani = ['slide-top']
  77. this.transClass = {
  78. 'position': 'fixed',
  79. 'left': 0,
  80. 'right': 0,
  81. }
  82. break
  83. case 'left':
  84. this.ani = ['slide-left']
  85. this.transClass = {
  86. 'position': 'fixed',
  87. 'top': 0,
  88. 'left': 0,
  89. 'bottom': 0,
  90. }
  91. break
  92. case 'right':
  93. this.ani = ['slide-right']
  94. this.transClass = {
  95. 'position': 'fixed',
  96. 'top': 0,
  97. 'right': 0,
  98. 'bottom': 0,
  99. }
  100. break
  101. case 'bottom':
  102. this.ani = ['slide-bottom']
  103. this.transClass = {
  104. 'position': 'fixed',
  105. 'left': 0,
  106. 'right': 0,
  107. 'bottom': "0px"
  108. }
  109. break
  110. case 'center':
  111. this.ani = ['zoom-out', 'fade']
  112. this.transClass = {
  113. 'position': 'fixed',
  114. /* #ifndef APP-NVUE */
  115. 'display': 'flex',
  116. 'flexDirection': 'column',
  117. /* #endif */
  118. 'bottom': 0,
  119. 'left': 0,
  120. 'right': 0,
  121. 'top': 0,
  122. 'justifyContent': 'center',
  123. 'alignItems': 'center'
  124. }
  125. break
  126. }
  127. },
  128. immediate: true
  129. }
  130. },
  131. created() {
  132. if (this.animation) {
  133. this.duration = 300
  134. } else {
  135. this.duration = 0
  136. }
  137. },
  138. methods: {
  139. clear(e) {
  140. // TODO nvue 取消冒泡
  141. e.stopPropagation()
  142. },
  143. open() {
  144. this.showPopup = true
  145. this.$nextTick(() => {
  146. clearTimeout(this.timer)
  147. this.timer = setTimeout(() => {
  148. this.showTrans = true
  149. }, 50);
  150. })
  151. this.$emit('change', {
  152. show: true
  153. })
  154. },
  155. close(type) {
  156. this.showTrans = false
  157. this.$nextTick(() => {
  158. clearTimeout(this.timer)
  159. this.timer = setTimeout(() => {
  160. this.$emit('change', {
  161. show: false
  162. })
  163. this.showPopup = false
  164. }, 300)
  165. })
  166. },
  167. onTap() {
  168. if (!this.maskClick) return
  169. this.close()
  170. }
  171. }
  172. }
  173. </script>
  174. <style lang="scss" scoped>
  175. .uni-popup {
  176. position: fixed;
  177. /* #ifdef H5 */
  178. top: var(--window-top);
  179. /* #endif */
  180. /* #ifndef H5 */
  181. top: 0;
  182. /* #endif */
  183. bottom: -90px;
  184. left: 0;
  185. right: 0;
  186. /* #ifndef APP-NVUE */
  187. z-index: 900;
  188. /* #endif */
  189. }
  190. .uni-popup__mask {
  191. position: absolute;
  192. top: 0;
  193. bottom: 0;
  194. left: 0;
  195. right: 0;
  196. background-color: $uni-bg-color-mask;
  197. opacity: 0;
  198. }
  199. .mask-ani {
  200. transition-property: opacity;
  201. transition-duration: 0.2s;
  202. }
  203. .uni-top-mask {
  204. opacity: 1;
  205. }
  206. .uni-bottom-mask {
  207. opacity: 1;
  208. }
  209. .uni-center-mask {
  210. opacity: 1;
  211. }
  212. .uni-popup__wrapper {
  213. /* #ifndef APP-NVUE */
  214. display: block;
  215. /* #endif */
  216. position: absolute;
  217. }
  218. .top {
  219. top: 0;
  220. left: 0;
  221. right: 0;
  222. transform: translateY(-500px);
  223. }
  224. .bottom {
  225. bottom: 0;
  226. left: 0;
  227. right: 0;
  228. transform: translateY(500px);
  229. }
  230. .center {
  231. /* #ifndef APP-NVUE */
  232. display: flex;
  233. flex-direction: column;
  234. /* #endif */
  235. bottom: 0;
  236. left: 0;
  237. right: 0;
  238. top: 0;
  239. justify-content: center;
  240. align-items: center;
  241. transform: scale(1.2);
  242. opacity: 0;
  243. }
  244. .uni-popup__wrapper-box {
  245. /* #ifndef APP-NVUE */
  246. display: block;
  247. /* #endif */
  248. position: relative;
  249. }
  250. .content-ani {
  251. // transition: transform 0.3s;
  252. transition-property: transform, opacity;
  253. transition-duration: 0.2s;
  254. }
  255. .uni-top-content {
  256. transform: translateY(0);
  257. }
  258. .uni-bottom-content {
  259. transform: translateY(0);
  260. }
  261. .uni-center-content {
  262. transform: scale(1);
  263. opacity: 1;
  264. }
  265. </style>