uni-app 自定义tabbar

发布于:2024-05-09 ⋅ 阅读:(22) ⋅ 点赞:(0)

uni-app 自定义tabbar

第一步:在components下创建目录tabbar,再在tabbar目录下创建tabbar.vue文件

<template>
	<view class="tabbar jsf-around">
		<view class="tabbar-item dis-c align-center " v-for="(item,index) in list" :key="index" @click="changeTab(index)">

			<view class="dis-c align-center " v-if="current == index">
				<view class="xian"></view>
				<view class="dis-c align-center padding-tb-20">
					<image :style="{width:item.width, height:item.height}" :src="item.selectedIconPath"></image>
					<view class="text active font-pf-b margin-t-5">{{item.text}}</view>
				</view>
			</view>

			<view v-else>
				<view class="wu_xian"></view>
				<view class="dis-c align-center padding-tb-20">
					<image :style="{width:item.width, height:item.height}" :src="item.iconPath"></image>
					<view class="text font-pf-b margin-t-5">{{item.text}}</view>
				</view>
			</view>

			<!-- <image class="img" :src="item.selectedIconPath" v-if="current == index"></image>
			<image class="img" :src="item.iconPath" v-else></image>
			<view class="text active font-pf-b " v-if="current == index">{{item.text}}</view>
			<view class="text font-pf-b " v-else>{{item.text}}</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		name: "tabbar",
		props: {
			current: String
		},
		created() {
			uni.hideTabBar()
		},
		data() {
			return {
				list: [{
						pagePath: "pages/index/index",
						iconPath: "../../static/tabbar/shouye.png",
						selectedIconPath: "../../static/tabbar/shouye(lan).png",
						text: "首页",
						width: '48rpx',
						height: '51rpx',
					}, {
						pagePath: "pages/tongji/tongji",
						iconPath: "../../static/tabbar/tongji.png",
						selectedIconPath: "../../static/tabbar/tongji(lan).png",
						text: "统计",
						width: '47rpx',
						height: '51rpx',
					},
					{
						pagePath: "pages/xiaoxi/xiaoxi",
						iconPath: "../../static/tabbar/xiaoxi.png",
						selectedIconPath: "../../static/tabbar/xiaoxi(lan).png",
						text: "消息",
						width: '51rpx',
						height: '51rpx',
					},
					{
						pagePath: "pages/wode/wode",
						iconPath: "../../static/tabbar/wode.png",
						selectedIconPath: "../../static/tabbar/wode(lan).png",
						text: "我的",
						width: '44rpx',
						height: '51rpx'
					}
				]
			}
		},
		methods: {
			changeTab(e) {
				// console.log(e);
				uni.switchTab({
					url: '/' + this.list[e].pagePath,
				})
			}
		}
	}
</script>

<style scoped>
	.tabbar {
		position: absolute;
		bottom: 0;
		z-index: 99;
		width: 100%;
		background: #FFFFFF;
		box-shadow: 0px -9px 49px 0px rgba(212, 221, 236, 0.47);
		border-radius: 60rpx 60rpx 0px 0px;
		background-repeat: no-repeat;
		/* padding: 0 0 20rpx 0; */
	}

	,
	.tabbar-item {}

	.wu_xian {
		width: 30rpx;
		height: 8rpx;
		background: #fff;
	}

	.xian {
		width: 30rpx;
		height: 8rpx;
		background: #1C65F3;
	}

	/* .img {
		width: 45upx;
		height: 48upx;
	} */

	.text {
		font-size: 24upx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #bbb;
		/* line-height: 27upx; */
	}

	.text.active {
		color: #1061F5;
	}
</style>

第二步:在page下创建index目录,创建index.vue文件,调用自定义组件tabbar

<template>
	<view>
		<tabbar current="0"></tabbar>
		</view>
</template>
<script>
	import tabbar from '@/components/tabbar/tabbar'  //引用组件中的tabbar
	export default {
		components: {
			tabbar	//引用组件中的tabbar
		},
		data() {
			return {
			}
		},

		created() {
			uni.hideTabBar()  //隐藏掉原生tabbar组件
		},
		methods: {
		}
	}
</script>