¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view v-if="showPopup" class="uni-popup" :class="[popupstyle, isDesktop ? 'fixforpc-z-index' : '']"> |
| | | <view @touchstart="touchstart"> |
| | | <uni-transition key="1" v-if="maskShow" name="mask" mode-class="fade" :styles="maskClass" |
| | | :duration="duration" :show="showTrans" @click="onTap" /> |
| | | <uni-transition key="2" :mode-class="ani" name="content" :styles="transClass" :duration="duration" |
| | | :show="showTrans" @click="onTap"> |
| | | <view class="uni-popup__wrapper" :style="{ backgroundColor: bg }" :class="[popupstyle]" @click="clear"> |
| | | <slot /> |
| | | </view> |
| | | </uni-transition> |
| | | </view> |
| | | <!-- #ifdef H5 --> |
| | | <keypress v-if="maskShow" @esc="onTap" /> |
| | | <!-- #endif --> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | // #ifdef H5 |
| | | import keypress from './keypress.js' |
| | | // #endif |
| | | |
| | | /** |
| | | * PopUp å¼¹åºå± |
| | | * @description å¼¹åºå±ç»ä»¶ï¼ä¸ºäºè§£å³é®ç½©å¼¹å±çé®é¢ |
| | | * @tutorial https://ext.dcloud.net.cn/plugin?id=329 |
| | | * @property {String} type = [top|center|bottom|left|right|message|dialog|share] å¼¹åºæ¹å¼ |
| | | * @value top é¡¶é¨å¼¹åº |
| | | * @value center ä¸é´å¼¹åº |
| | | * @value bottom åºé¨å¼¹åº |
| | | * @value left å·¦ä¾§å¼¹åº |
| | | * @value right å³ä¾§å¼¹åº |
| | | * @value message æ¶æ¯æç¤º |
| | | * @value dialog å¯¹è¯æ¡ |
| | | * @value share åºé¨åäº«ç¤ºä¾ |
| | | * @property {Boolean} animation = [true|false] æ¯å¦å¼å¯å¨ç» |
| | | * @property {Boolean} maskClick = [true|false] èçç¹å»æ¯å¦å
³éå¼¹çª(åºå¼) |
| | | * @property {Boolean} isMaskClick = [true|false] èçç¹å»æ¯å¦å
³éå¼¹çª |
| | | * @property {String} backgroundColor 主çªå£èæ¯è² |
| | | * @property {String} maskBackgroundColor èçé¢è² |
| | | * @property {Boolean} safeArea æ¯å¦éé
åºé¨å®å
¨åº |
| | | * @event {Function} change æå¼å
³éå¼¹çªè§¦åï¼e={show: false} |
| | | * @event {Function} maskClick ç¹å»é®ç½©è§¦å |
| | | */ |
| | | |
| | | export default { |
| | | name: 'uniPopup', |
| | | components: { |
| | | // #ifdef H5 |
| | | keypress |
| | | // #endif |
| | | }, |
| | | emits: ['change', 'maskClick'], |
| | | props: { |
| | | // å¼å¯å¨ç» |
| | | animation: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | // å¼¹åºå±ç±»åï¼å¯éå¼ï¼top: é¡¶é¨å¼¹åºå±ï¼bottomï¼åºé¨å¼¹åºå±ï¼centerï¼å
¨å±å¼¹åºå± |
| | | // message: æ¶æ¯æç¤º ; dialog : å¯¹è¯æ¡ |
| | | type: { |
| | | type: String, |
| | | default: 'center' |
| | | }, |
| | | // maskClick |
| | | isMaskClick: { |
| | | type: Boolean, |
| | | default: null |
| | | }, |
| | | // TODO 2 ä¸ªçæ¬ååºå¼å±æ§ ï¼ä½¿ç¨ isMaskClick |
| | | maskClick: { |
| | | type: Boolean, |
| | | default: null |
| | | }, |
| | | backgroundColor: { |
| | | type: String, |
| | | default: 'none' |
| | | }, |
| | | safeArea: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | maskBackgroundColor: { |
| | | type: String, |
| | | default: 'rgba(0, 0, 0, 0.4)' |
| | | }, |
| | | }, |
| | | |
| | | watch: { |
| | | /** |
| | | * çå¬typeç±»å |
| | | */ |
| | | type: { |
| | | handler: function(type) { |
| | | if (!this.config[type]) return |
| | | this[this.config[type]](true) |
| | | }, |
| | | immediate: true |
| | | }, |
| | | isDesktop: { |
| | | handler: function(newVal) { |
| | | if (!this.config[newVal]) return |
| | | this[this.config[this.type]](true) |
| | | }, |
| | | immediate: true |
| | | }, |
| | | /** |
| | | * çå¬é®ç½©æ¯å¦å¯ç¹å» |
| | | * @param {Object} val |
| | | */ |
| | | maskClick: { |
| | | handler: function(val) { |
| | | this.mkclick = val |
| | | }, |
| | | immediate: true |
| | | }, |
| | | isMaskClick: { |
| | | handler: function(val) { |
| | | this.mkclick = val |
| | | }, |
| | | immediate: true |
| | | }, |
| | | // H5 ä¸ç¦æ¢åºé¨æ»å¨ |
| | | showPopup(show) { |
| | | // #ifdef H5 |
| | | // fix by mehaotian å¤ç h5 æ»å¨ç©¿éçé®é¢ |
| | | document.getElementsByTagName('body')[0].style.overflow = show ? 'hidden' : 'visible' |
| | | // #endif |
| | | } |
| | | }, |
| | | data() { |
| | | return { |
| | | duration: 300, |
| | | ani: [], |
| | | showPopup: false, |
| | | showTrans: false, |
| | | popupWidth: 0, |
| | | popupHeight: 0, |
| | | config: { |
| | | top: 'top', |
| | | bottom: 'bottom', |
| | | center: 'center', |
| | | left: 'left', |
| | | right: 'right', |
| | | message: 'top', |
| | | dialog: 'center', |
| | | share: 'bottom' |
| | | }, |
| | | 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: this.isDesktop ? 'fixforpc-top' : 'top' |
| | | } |
| | | }, |
| | | computed: { |
| | | isDesktop() { |
| | | return this.popupWidth >= 500 && this.popupHeight >= 500 |
| | | }, |
| | | bg() { |
| | | if (this.backgroundColor === '' || this.backgroundColor === 'none') { |
| | | return 'transparent' |
| | | } |
| | | return this.backgroundColor |
| | | } |
| | | }, |
| | | mounted() { |
| | | const fixSize = () => { |
| | | const { |
| | | windowWidth, |
| | | windowHeight, |
| | | windowTop, |
| | | safeArea, |
| | | screenHeight, |
| | | safeAreaInsets |
| | | } = uni.getSystemInfoSync() |
| | | this.popupWidth = windowWidth |
| | | this.popupHeight = windowHeight + (windowTop || 0) |
| | | // TODO fix by mehaotian æ¯å¦éé
åºé¨å®å
¨åº ,ç®å微信ios ãå app ios è®¡ç®æå·®å¼ï¼éè¦æ¡æ¶ä¿®å¤ |
| | | if (safeArea && this.safeArea) { |
| | | // #ifdef MP-WEIXIN |
| | | this.safeAreaInsets = screenHeight - safeArea.bottom |
| | | // #endif |
| | | // #ifndef MP-WEIXIN |
| | | this.safeAreaInsets = safeAreaInsets.bottom |
| | | // #endif |
| | | } else { |
| | | this.safeAreaInsets = 0 |
| | | } |
| | | } |
| | | fixSize() |
| | | // #ifdef H5 |
| | | // window.addEventListener('resize', fixSize) |
| | | // this.$once('hook:beforeDestroy', () => { |
| | | // window.removeEventListener('resize', fixSize) |
| | | // }) |
| | | // #endif |
| | | }, |
| | | // #ifndef VUE3 |
| | | // TODO vue2 |
| | | destroyed() { |
| | | this.setH5Visible() |
| | | }, |
| | | // #endif |
| | | // #ifdef VUE3 |
| | | // TODO vue3 |
| | | unmounted() { |
| | | this.setH5Visible() |
| | | }, |
| | | // #endif |
| | | created() { |
| | | // this.mkclick = this.isMaskClick || this.maskClick |
| | | if (this.isMaskClick === null && this.maskClick === null) { |
| | | this.mkclick = true |
| | | } else { |
| | | this.mkclick = this.isMaskClick !== null ? this.isMaskClick : this.maskClick |
| | | } |
| | | if (this.animation) { |
| | | this.duration = 300 |
| | | } else { |
| | | this.duration = 0 |
| | | } |
| | | // TODO å¤ç message ç»ä»¶çå½å¨æå¼å¸¸çé®é¢ |
| | | this.messageChild = null |
| | | // TODO è§£å³å¤´æ¡å泡çé®é¢ |
| | | this.clearPropagation = false |
| | | this.maskClass.backgroundColor = this.maskBackgroundColor |
| | | }, |
| | | methods: { |
| | | setH5Visible() { |
| | | // #ifdef H5 |
| | | // fix by mehaotian å¤ç h5 æ»å¨ç©¿éçé®é¢ |
| | | document.getElementsByTagName('body')[0].style.overflow = 'visible' |
| | | // #endif |
| | | }, |
| | | /** |
| | | * å
¬ç¨æ¹æ³ï¼ä¸æ¾ç¤ºé®ç½©å± |
| | | */ |
| | | closeMask() { |
| | | this.maskShow = false |
| | | }, |
| | | /** |
| | | * å
¬ç¨æ¹æ³ï¼é®ç½©å±ç¦æ¢ç¹å» |
| | | */ |
| | | disableMask() { |
| | | this.mkclick = false |
| | | }, |
| | | // TODO nvue åæ¶åæ³¡ |
| | | clear(e) { |
| | | // #ifndef APP-NVUE |
| | | e.stopPropagation() |
| | | // #endif |
| | | this.clearPropagation = true |
| | | }, |
| | | |
| | | open(direction) { |
| | | // fix by mehaotian å¤çå¿«éæå¼å
³éçæ
åµ |
| | | if (this.showPopup) { |
| | | clearTimeout(this.timer) |
| | | this.showPopup = false |
| | | } |
| | | let innerType = ['top', 'center', 'bottom', 'left', 'right', 'message', 'dialog', 'share'] |
| | | if (!(direction && innerType.indexOf(direction) !== -1)) { |
| | | direction = this.type |
| | | } |
| | | if (!this.config[direction]) { |
| | | console.error('缺å°ç±»åï¼', direction) |
| | | return |
| | | } |
| | | this[this.config[direction]]() |
| | | this.$emit('change', { |
| | | show: true, |
| | | type: direction |
| | | }) |
| | | }, |
| | | close(type) { |
| | | this.showTrans = false |
| | | this.$emit('change', { |
| | | show: false, |
| | | type: this.type |
| | | }) |
| | | clearTimeout(this.timer) |
| | | // // èªå®ä¹å
³éäºä»¶ |
| | | // this.customOpen && this.customClose() |
| | | this.timer = setTimeout(() => { |
| | | this.showPopup = false |
| | | }, 300) |
| | | }, |
| | | // TODO å¤çåæ³¡äºä»¶ï¼å¤´æ¡çåæ³¡äºä»¶æé®é¢ ï¼å
è¿æ ·å
¼å®¹ |
| | | touchstart() { |
| | | this.clearPropagation = false |
| | | }, |
| | | |
| | | onTap() { |
| | | if (this.clearPropagation) { |
| | | // fix by mehaotian å
¼å®¹ nvue |
| | | this.clearPropagation = false |
| | | return |
| | | } |
| | | this.$emit('maskClick') |
| | | if (!this.mkclick) return |
| | | this.close() |
| | | }, |
| | | /** |
| | | * é¡¶é¨å¼¹åºæ ·å¼å¤ç |
| | | */ |
| | | top(type) { |
| | | this.popupstyle = this.isDesktop ? 'fixforpc-top' : 'top' |
| | | this.ani = ['slide-top'] |
| | | this.transClass = { |
| | | position: 'fixed', |
| | | left: 0, |
| | | right: 0, |
| | | backgroundColor: this.bg |
| | | } |
| | | // TODO å
¼å®¹ type 屿§ ï¼åç»ä¼åºå¼ |
| | | if (type) return |
| | | this.showPopup = true |
| | | this.showTrans = true |
| | | this.$nextTick(() => { |
| | | if (this.messageChild && this.type === 'message') { |
| | | this.messageChild.timerClose() |
| | | } |
| | | }) |
| | | }, |
| | | /** |
| | | * åºé¨å¼¹åºæ ·å¼å¤ç |
| | | */ |
| | | bottom(type) { |
| | | this.popupstyle = 'bottom' |
| | | this.ani = ['slide-bottom'] |
| | | this.transClass = { |
| | | position: 'fixed', |
| | | left: 0, |
| | | right: 0, |
| | | bottom: 0, |
| | | paddingBottom: this.safeAreaInsets + 'px', |
| | | backgroundColor: this.bg |
| | | } |
| | | // TODO å
¼å®¹ type 屿§ ï¼åç»ä¼åºå¼ |
| | | if (type) return |
| | | this.showPopup = true |
| | | this.showTrans = true |
| | | }, |
| | | /** |
| | | * ä¸é´å¼¹åºæ ·å¼å¤ç |
| | | */ |
| | | center(type) { |
| | | 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' |
| | | } |
| | | // TODO å
¼å®¹ type 屿§ ï¼åç»ä¼åºå¼ |
| | | if (type) return |
| | | this.showPopup = true |
| | | this.showTrans = true |
| | | }, |
| | | left(type) { |
| | | this.popupstyle = 'left' |
| | | this.ani = ['slide-left'] |
| | | this.transClass = { |
| | | position: 'fixed', |
| | | left: 0, |
| | | bottom: 0, |
| | | top: 0, |
| | | backgroundColor: this.bg, |
| | | /* #ifndef APP-NVUE */ |
| | | display: 'flex', |
| | | flexDirection: 'column' |
| | | /* #endif */ |
| | | } |
| | | // TODO å
¼å®¹ type 屿§ ï¼åç»ä¼åºå¼ |
| | | if (type) return |
| | | this.showPopup = true |
| | | this.showTrans = true |
| | | }, |
| | | right(type) { |
| | | this.popupstyle = 'right' |
| | | this.ani = ['slide-right'] |
| | | this.transClass = { |
| | | position: 'fixed', |
| | | bottom: 0, |
| | | right: 0, |
| | | top: 0, |
| | | backgroundColor: this.bg, |
| | | /* #ifndef APP-NVUE */ |
| | | display: 'flex', |
| | | flexDirection: 'column' |
| | | /* #endif */ |
| | | } |
| | | // TODO å
¼å®¹ type 屿§ ï¼åç»ä¼åºå¼ |
| | | if (type) return |
| | | this.showPopup = true |
| | | this.showTrans = true |
| | | } |
| | | } |
| | | } |
| | | </script> |
| | | <style lang="scss"> |
| | | .uni-popup { |
| | | position: fixed; |
| | | /* #ifndef APP-NVUE */ |
| | | z-index: 99; |
| | | |
| | | /* #endif */ |
| | | &.top, |
| | | &.left, |
| | | &.right { |
| | | /* #ifdef H5 */ |
| | | top: var(--window-top); |
| | | /* #endif */ |
| | | /* #ifndef H5 */ |
| | | top: 0; |
| | | /* #endif */ |
| | | } |
| | | |
| | | .uni-popup__wrapper { |
| | | /* #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 */ |
| | | &.left, |
| | | &.right { |
| | | /* #ifdef H5 */ |
| | | padding-top: var(--window-top); |
| | | /* #endif */ |
| | | /* #ifndef H5 */ |
| | | padding-top: 0; |
| | | /* #endif */ |
| | | flex: 1; |
| | | } |
| | | } |
| | | } |
| | | |
| | | .fixforpc-z-index { |
| | | /* #ifndef APP-NVUE */ |
| | | z-index: 999; |
| | | /* #endif */ |
| | | } |
| | | |
| | | .fixforpc-top { |
| | | top: 0; |
| | | } |
| | | </style> |