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