uni-popup.vue 5.8 KB

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