uniapp自定义底部tabBar

使用场景:在一个非tabbar页面,想要有底部导航效果,故自定义效果,系统原底部导航栏仍在正常使用

效果:

布局:

<template>
	<view class="tab-bar" :style="{height: height + 'px'}">
		<view v-for="(item, index) in tabBars" :key="index" @click="switchTab(item)"
			:style="{color: index === selectIndex ? item.selectedTextColor : item.textColor}">
			<view style="display: flex; justify-content: center;">
				<image :src="index === selectIndex ? item.selectedIconPath : item.iconPath" style="height: 25px; width: 25px;"></image>
			</view>
			<view style="margin-top: 2px; font-size: 12px;">
				{{item.text}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"tabBar",
		props: {
			tabBars: {
				type: Array,
				required: true
			},
			selectIndex: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				height: undefined
			};
		},
		methods: {
			switchTab(item) {
				if (this.getCurrentPageIndex() !== item.pagePath) {
					uni.redirectTo({
						url: item.pagePath
					});
				}
			},
			getCurrentPageIndex() {
				const pages = getCurrentPages();
				return pages[pages.length - 1].route;
			}
		},
		mounted() {
			let systemInfo = uni.getSystemInfoSync();
			let tabBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - systemInfo.statusBarHeight;
			this.height = tabBarHeight
		}
	}
</script>

<style>
.tab-bar {
  display: flex;
  justify-content: space-around;
  position: fixed;
  height: 5vh;
  bottom: 0;
  width: 100%;
  background-color: #fbfcfe;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  padding-top: 3px;
  padding-bottom: 2px;
}
</style>

使用:

<tabBar :tab-bars="approvalTabbar" :selectIndex="1"></tabBar>
// selectIndex 是当前页面的数组下标
approvalTabbar = [
	{
		iconPath: '/static/icon/mess.png',
		selectedIconPath:'/static/icon/mess-selected.png',
		text: "发起申请",
		pagePath:'/pages/approval/launch/launch',
		textColor: tabTextColor,
		selectedTextColor: tabSelectedTextColor
	},
	{
		iconPath: '/static/icon/approval-mine.png',
		selectedIconPath:'/static/icon/approval-mine-selected.png',
		text: "我审批的",
		pagePath:'/pages/approval/accraditation/accraditation',
		textColor: tabTextColor,
		selectedTextColor: tabSelectedTextColor
	},
	{
		iconPath: '/static/icon/submit.png',
		selectedIconPath:'/static/icon/submit-selected.png',
		text: "已提交",
		pagePath:'/pages/approval/submit/submit',
		textColor: tabTextColor,
		selectedTextColor: tabSelectedTextColor
	}
]

不足之处:

1.需要在参与跳转的每个页面引入组件

2.引入时需要传递相同的tabs数组数据、页面与数组对应的下标

3.页面跳转后会重绘底部导航栏,出现闪的问题

UniApp中,可以通过自定义底部TabBar来实现自定义底部导航栏。以下是一些步骤来实现自定义底部TabBar: 1. 创建一个底部TabBar组件:在components文件夹中创建一个名为TabBar的组件,用于显示底部导航栏的内容。 2. 在App.vue中引入TabBar组件:在App.vue文件中引入TabBar组件,并在页面中使用该组件。 3. 定义底部导航栏的数据:在TabBar组件中定义一个数组,包含每个底部导航栏的图标、文字等信息。 4. 使用循环渲染底部导航栏:在TabBar组件的template中使用v-for指令,循环渲染每个底部导航栏项。 5. 添加点击事件:为每个底部导航栏项添加点击事件,用于切换页面或执行其他操作。 6. 在页面中使用自定义底部TabBar:在需要显示底部导航栏的页面中,使用自定义底部TabBar组件。 以下是一个简单的示例代码: ```vue <!-- TabBar.vue --> <template> <view class="tab-bar"> <view v-for="(item, index) in tabBarList" :key="index" class="tab-bar-item" :class="{ active: activeIndex === index }" @click="handleTabClick(index)" > <image :src="item.icon" class="tab-bar-icon" /> <text class="tab-bar-text">{{ item.text }}</text> </view> </view> </template> <script> export default { data() { return { tabBarList: [ { icon: 'home.png', text: '首页' }, { icon: 'category.png', text: '分类' }, { icon: 'cart.png', text: '购物车' }, { icon: 'user.png', text: '我的' }, ], activeIndex: 0, }; }, methods: { handleTabClick(index) { this.activeIndex = index; // 执行切换页面或其他操作 }, }, }; </script> <!-- App.vue --> <template> <view class="app"> <router-view /> <TabBar /> </view> </template> <script> import TabBar from '@/components/TabBar.vue'; export default { components: { TabBar, }, }; </script> ``` 在以上示例中,TabBar组件循环渲染了底部导航栏的每个项,并通过点击事件切换页面或执行其他操作。在App.vue中使用了TabBar组件,并在页面中显示底部导航栏。 你可以根据自己的需求,对TabBar组件进行样式和功能的定制,以满足自定义底部TabBar的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值