¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view v-if="visibleSync" :class="{ 'uni-drawer--visible': showDrawer }" class="uni-drawer" @touchmove.stop.prevent="clear"> |
| | | <view class="uni-drawer__mask" :class="{ 'uni-drawer__mask--visible': showDrawer && mask }" @tap="close('mask')" /> |
| | | <view class="uni-drawer__content" :class="{'uni-drawer--right': rightMode,'uni-drawer--left': !rightMode, 'uni-drawer__content--visible': showDrawer}" :style="{width:drawerWidth+'px'}"> |
| | | <slot /> |
| | | </view> |
| | | <!-- #ifdef H5 --> |
| | | <keypress @esc="close('mask')" /> |
| | | <!-- #endif --> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | // #ifdef H5 |
| | | import keypress from './keypress.js' |
| | | // #endif |
| | | /** |
| | | * Drawer æ½å± |
| | | * @description æ½å±ä¾§æ»èå |
| | | * @tutorial https://ext.dcloud.net.cn/plugin?id=26 |
| | | * @property {Boolean} mask = [true | false] æ¯å¦æ¾ç¤ºé®ç½© |
| | | * @property {Boolean} maskClick = [true | false] ç¹å»é®ç½©æ¯å¦å
³é |
| | | * @property {Boolean} mode = [left | right] Drawer æ»åºä½ç½® |
| | | * @value left ä»å·¦ä¾§æ»åº |
| | | * @value right ä»å³ä¾§ä¾§æ»åº |
| | | * @property {Number} width æ½å±ç宽度 ï¼ä»
vue 页é¢çæ |
| | | * @event {Function} close ç»ä»¶å
³éæ¶è§¦åäºä»¶ |
| | | */ |
| | | export default { |
| | | name: 'UniDrawer', |
| | | components: { |
| | | // #ifdef H5 |
| | | keypress |
| | | // #endif |
| | | }, |
| | | emits:['change'], |
| | | props: { |
| | | /** |
| | | * æ¾ç¤ºæ¨¡å¼ï¼å·¦ãå³ï¼ï¼åªå¨åå§åçæ |
| | | */ |
| | | mode: { |
| | | type: String, |
| | | default: '' |
| | | }, |
| | | /** |
| | | * è屿¾ç¤ºç¶æ |
| | | */ |
| | | mask: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | /** |
| | | * é®ç½©æ¯å¦å¯ç¹å»å
³é |
| | | */ |
| | | maskClick:{ |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | /** |
| | | * æ½å±å®½åº¦ |
| | | */ |
| | | width: { |
| | | type: Number, |
| | | default: 220 |
| | | } |
| | | }, |
| | | data() { |
| | | return { |
| | | visibleSync: false, |
| | | showDrawer: false, |
| | | rightMode: false, |
| | | watchTimer: null, |
| | | drawerWidth: 220 |
| | | } |
| | | }, |
| | | created() { |
| | | // #ifndef APP-NVUE |
| | | this.drawerWidth = this.width |
| | | // #endif |
| | | this.rightMode = this.mode === 'right' |
| | | }, |
| | | methods: { |
| | | clear(){}, |
| | | close(type) { |
| | | // fixed by mehaotian æ½å±å°æªå®å
¨å
³éæé®ç½©ç¦æ¢ç¹å»æ¶ä¸è§¦å以ä¸é»è¾ |
| | | if((type === 'mask' && !this.maskClick) || !this.visibleSync) return |
| | | this._change('showDrawer', 'visibleSync', false) |
| | | }, |
| | | open() { |
| | | // fixed by mehaotian å¤çéå¤ç¹å»æå¼çäºä»¶ |
| | | if(this.visibleSync) return |
| | | this._change('visibleSync', 'showDrawer', true) |
| | | }, |
| | | _change(param1, param2, status) { |
| | | this[param1] = status |
| | | if (this.watchTimer) { |
| | | clearTimeout(this.watchTimer) |
| | | } |
| | | this.watchTimer = setTimeout(() => { |
| | | this[param2] = status |
| | | this.$emit('change',status) |
| | | }, status ? 50 : 300) |
| | | } |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" > |
| | | $uni-mask: rgba($color: #000000, $alpha: 0.4) ; |
| | | // æ½å±å®½åº¦ |
| | | $drawer-width: 220px; |
| | | |
| | | .uni-drawer { |
| | | /* #ifndef APP-NVUE */ |
| | | display: block; |
| | | /* #endif */ |
| | | position: fixed; |
| | | top: 0; |
| | | left: 0; |
| | | right: 0; |
| | | bottom: 0; |
| | | overflow: hidden; |
| | | z-index: 999; |
| | | } |
| | | |
| | | .uni-drawer__content { |
| | | /* #ifndef APP-NVUE */ |
| | | display: block; |
| | | /* #endif */ |
| | | position: absolute; |
| | | top: 0; |
| | | width: $drawer-width; |
| | | bottom: 0; |
| | | background-color: $uni-bg-color; |
| | | transition: transform 0.3s ease; |
| | | } |
| | | |
| | | .uni-drawer--left { |
| | | left: 0; |
| | | /* #ifdef APP-NVUE */ |
| | | transform: translateX(-$drawer-width); |
| | | /* #endif */ |
| | | /* #ifndef APP-NVUE */ |
| | | transform: translateX(-100%); |
| | | /* #endif */ |
| | | } |
| | | |
| | | .uni-drawer--right { |
| | | right: 0; |
| | | /* #ifdef APP-NVUE */ |
| | | transform: translateX($drawer-width); |
| | | /* #endif */ |
| | | /* #ifndef APP-NVUE */ |
| | | transform: translateX(100%); |
| | | /* #endif */ |
| | | } |
| | | |
| | | .uni-drawer__content--visible { |
| | | transform: translateX(0px); |
| | | } |
| | | |
| | | |
| | | .uni-drawer__mask { |
| | | /* #ifndef APP-NVUE */ |
| | | display: block; |
| | | /* #endif */ |
| | | opacity: 0; |
| | | position: absolute; |
| | | top: 0; |
| | | left: 0; |
| | | bottom: 0; |
| | | right: 0; |
| | | background-color: $uni-mask; |
| | | transition: opacity 0.3s; |
| | | } |
| | | |
| | | .uni-drawer__mask--visible { |
| | | /* #ifndef APP-NVUE */ |
| | | display: block; |
| | | /* #endif */ |
| | | opacity: 1; |
| | | } |
| | | </style> |