uni-popup.vue 5.9 KB

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