uni-popup.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <view v-if="showPopup" class="uni-popup" :class="[popupstyle]" @touchmove.stop.prevent="clear">
  3. <uni-transition v-if="maskShow" :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(val) {
  74. this.mkclick = val
  75. }
  76. },
  77. data() {
  78. return {
  79. duration: 300,
  80. ani: [],
  81. showPopup: false,
  82. showTrans: false,
  83. maskClass: {
  84. 'position': 'fixed',
  85. 'bottom': 0,
  86. 'top': 0,
  87. 'left': 0,
  88. 'right': 0,
  89. 'backgroundColor': 'rgba(0, 0, 0, 0.4)'
  90. },
  91. transClass: {
  92. 'position': 'fixed',
  93. 'left': 0,
  94. 'right': 0,
  95. },
  96. maskShow: true,
  97. mkclick: true,
  98. popupstyle: 'top'
  99. }
  100. },
  101. created() {
  102. this.mkclick = this.maskClick
  103. if (this.animation) {
  104. this.duration = 300
  105. } else {
  106. this.duration = 0
  107. }
  108. },
  109. methods: {
  110. clear(e) {
  111. // TODO nvue 取消冒泡
  112. e.stopPropagation()
  113. },
  114. open() {
  115. this.showPopup = true
  116. this.$nextTick(() => {
  117. new Promise(resolve => {
  118. clearTimeout(this.timer)
  119. this.timer = setTimeout(() => {
  120. this.showTrans = true
  121. // fixed by mehaotian 兼容 app 端
  122. this.$nextTick(() => {
  123. resolve();
  124. })
  125. }, 50);
  126. }).then(res => {
  127. // 自定义打开事件
  128. clearTimeout(this.msgtimer)
  129. this.msgtimer = setTimeout(() => {
  130. this.customOpen && this.customOpen()
  131. }, 100)
  132. this.$emit('change', {
  133. show: true,
  134. type: this.type
  135. })
  136. })
  137. })
  138. },
  139. close(type) {
  140. this.showTrans = false
  141. this.$nextTick(() => {
  142. this.$emit('change', {
  143. show: false,
  144. type: this.type
  145. })
  146. clearTimeout(this.timer)
  147. // 自定义关闭事件
  148. this.customOpen && this.customClose()
  149. this.timer = setTimeout(() => {
  150. this.showPopup = false
  151. }, 300)
  152. })
  153. },
  154. onTap() {
  155. if (!this.mkclick) return
  156. this.close()
  157. },
  158. /**
  159. * 顶部弹出样式处理
  160. */
  161. top() {
  162. this.popupstyle = 'top'
  163. this.ani = ['slide-top']
  164. this.transClass = {
  165. 'position': 'fixed',
  166. 'left': 0,
  167. 'right': 0,
  168. }
  169. },
  170. /**
  171. * 底部弹出样式处理
  172. */
  173. bottom() {
  174. this.popupstyle = 'bottom'
  175. this.ani = ['slide-bottom']
  176. this.transClass = {
  177. 'position': 'fixed',
  178. 'left': 0,
  179. 'right': 0,
  180. 'bottom': 0
  181. }
  182. },
  183. /**
  184. * 中间弹出样式处理
  185. */
  186. center() {
  187. this.popupstyle = 'center'
  188. this.ani = ['zoom-out', 'fade']
  189. this.transClass = {
  190. 'position': 'fixed',
  191. /* #ifndef APP-NVUE */
  192. 'display': 'flex',
  193. 'flexDirection': 'column',
  194. /* #endif */
  195. 'bottom': 0,
  196. 'left': 0,
  197. 'right': 0,
  198. 'top': 0,
  199. 'justifyContent': 'center',
  200. 'alignItems': 'center'
  201. }
  202. }
  203. }
  204. }
  205. </script>
  206. <style lang="scss" scoped>
  207. .uni-popup {
  208. position: fixed;
  209. /* #ifndef APP-NVUE */
  210. z-index: 99;
  211. /* #endif */
  212. }
  213. .uni-popup__mask {
  214. position: absolute;
  215. top: 0;
  216. bottom: 0;
  217. left: 0;
  218. right: 0;
  219. background-color: $uni-bg-color-mask;
  220. opacity: 0;
  221. }
  222. .mask-ani {
  223. transition-property: opacity;
  224. transition-duration: 0.2s;
  225. }
  226. .uni-top-mask {
  227. opacity: 1;
  228. }
  229. .uni-bottom-mask {
  230. opacity: 1;
  231. }
  232. .uni-center-mask {
  233. opacity: 1;
  234. }
  235. .uni-popup__wrapper {
  236. /* #ifndef APP-NVUE */
  237. display: block;
  238. /* #endif */
  239. position: absolute;
  240. }
  241. .top {
  242. /* #ifdef H5 */
  243. top: var(--window-top);
  244. /* #endif */
  245. /* #ifndef H5 */
  246. top: 0;
  247. /* #endif */
  248. }
  249. .bottom {
  250. bottom: 0;
  251. }
  252. .uni-popup__wrapper-box {
  253. /* #ifndef APP-NVUE */
  254. display: block;
  255. /* #endif */
  256. position: relative;
  257. /* iphonex 等安全区设置,底部安全区适配 */
  258. /* #ifndef APP-NVUE */
  259. padding-bottom: constant(safe-area-inset-bottom);
  260. padding-bottom: env(safe-area-inset-bottom);
  261. /* #endif */
  262. }
  263. .content-ani {
  264. // transition: transform 0.3s;
  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>