uni-popup.vue 5.8 KB

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