uni-popup.vue 5.8 KB

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