uni-app drawer抽屜導航點擊出現側邊欄

官方的使用只能點擊一次出現後再次點擊失效。hello uni-app的例子我用了總是報錯,那個closeDrawer錯誤,我也是不夠熟悉,各種改都不對,最後自己重新寫了使用方法。代碼如下

友情提示,從抽屜跳轉再返回要注意打開抽屜,不然會對不上。

以下是官方drawer。

<template>
	<view v-if="visibleSync" :class="{ 'uni-drawer--visible': showDrawer, 'uni-drawer--right': rightMode }" class="uni-drawer">
		<view class="uni-drawer__mask" @tap="close" />
		<view class="uni-drawer__content">
			<slot />
		</view>
	</view>
</template>

<script>
	export default {
		name: 'UniDrawer',
		props: {
			/**
			 * 顯示狀態
			 */
			visible: {
				type: Boolean,
				default: false
			},
			/**
			 * 顯示模式(左、右),只在初始化生效
			 */
			mode: {
				type: String,
				default: ''
			},
			/**
			 * 蒙層顯示狀態
			 */
			mask: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				visibleSync: false,
				showDrawer: false,
				rightMode: false,
				closeTimer: null,
				watchTimer: null
			}
		},
		watch: {
			visible(val) {
				clearTimeout(this.watchTimer)
				setTimeout(() => {
					this.showDrawer = val
				}, 100)
				if (this.visibleSync) {
					clearTimeout(this.closeTimer)
				}
				if (val) {
					this.visibleSync = val
				} else {
					this.watchTimer = setTimeout(() => {
						this.visibleSync = val
					}, 300)
				}
			}
		},
		created() {
			this.visibleSync = this.visible
			setTimeout(() => {
				this.showDrawer = this.visible
			}, 100)
			this.rightMode = this.mode === 'right'
		},
		methods: {
			close() {
				this.showDrawer = false
				this.closeTimer = setTimeout(() => {
					this.visibleSync = false
					this.$emit('close')
				}, 200)
			},
			moveHandle() {},
		}
	}
</script>

<style>
	@charset "UTF-8";

	.uni-drawer {
		display: block;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		overflow: hidden;
		visibility: hidden;
		z-index: 999;
		height: 100%
	}

	.uni-drawer.uni-drawer--right .uni-drawer__content {
		left: auto;
		right: 0;
		transform: translatex(100%)
	}

	.uni-drawer.uni-drawer--visible {
		visibility: visible
	}

	.uni-drawer.uni-drawer--visible .uni-drawer__content {
		transform: translatex(0)
	}

	.uni-drawer.uni-drawer--visible .uni-drawer__mask {
		display: block;
		opacity: 1
	}

	.uni-drawer__mask {
		display: block;
		opacity: 0;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, .4);
		transition: opacity .3s
	}

	.uni-drawer__content {
		display: block;
		position: absolute;
		top: 0;
		left: 0;
		width: 75%;
		height: 100%;
		background: #fff;
		transition: all .3s ease-out;
		transform: translatex(-100%)
	}
</style>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章