uniapp 微信小程序自定义底部tabbar

本文介绍了如何在uniapp中自定义微信小程序的底部tabbar。首先,在pages.json配置基础tabbar,接着在app.vue的onLaunch方法中隐藏默认tabbar。然后创建自定义tabbar组件tabbar.vue,包含所需样式。最后在主页和更多页面引入并设置current属性以显示相应选中状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pages.json中的tabbar照常写;

然后在app.vue中的onLaunch方法中隐藏原生的tabbar:

uni.hideTabBar() 

创建tabbar组件,components = >  tabbar.vue

组件内容就是你要自定义的样式信息,如图:

 

 组件代码:

<template>
	<view class="tabbar" :style="{'padding-bottom': paddingBottomHeight + 'rpx'}">
		<view class="tabbar-item" v-for="(item, index) in list" :key="index" @click="tabbarChange(item.path)" :class="{'border-right':0 == index}">
			<image class="item-img" :src="item.icon_a" v-if="current == index"></image>
			<image class="item-img" :src="item.icon" v-else></image>
			<view class="item-name" :class="{'tabbarActive border-bottom': current == index}" v-if="item.text">
				{{item.text}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			current: String
		},
		data() {
			return {
				paddingBottomHeight: 0, //苹果X以上手机底部适配高度
				list: [{
					text: '主页',
					icon: '/static/img/home.png', //未选中图片
					icon_a: '/static/img/home_select.png', //选中图片
					path: "/pages/home/home", //页面路径
				}, {
					text: '更多',
					icon: '/static/img/more.png',
					icon_a: '/static/img/more_select.png',
					path: "/pages/more/more",
				}]
			};
		},
		created() {
			let that = this;
			uni.getSystemInfo({
				success: function(res) {
					let model = ['X', 'XR', 'XS', '11', '12', '13', '14', '15'];
					model.forEach(item => {
						//适配iphoneX以上的底部,给tabbar一定高度的padding-bottom
						if (res.model.indexOf(item) != -1 && res.model.indexOf('iPhone') != -1) {
							that.paddingBottomHeight = 40;
						}
					})
				}
			});
		},
		methods: {
            //这里就是点击跳转的页面
			tabbarChange(path) {
				uni.switchTab({
					url: path
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.tabbarActive {
		color: #060606 !important;
	}
	.border-bottom {
		border-bottom: 3px solid #707070;
	}
	.border-right{
		border-right:1px solid #707070;
	}
	.tabbar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		justify-content: space-around;
		align-items: center;
		width: 100%;
		height: 80rpx;
		padding-top: 10rpx;
		padding-bottom: 10rpx !important;
		background-color: #ffffff;

		.tabbar-item {
			display: flex;
			align-items: center;
			justify-content: center;
			height: 100%;
			width: 100%;
			.item-img {
				width: 40rpx;
				height: 40rpx;
			}
			.item-name {
				color: #060606;
				margin-left: 64rpx;
				// font-size: 26rpx;
				line-height: 70rpx;
				
			}

		}
	}
</style>

在 主页 和 更多 页面调用tabbar组件

主页调用写current = 0

更多调用写current = 1

<template>
	<view class="container">
		<view-tabbar :current="0"></view-tabbar>

	</view>
</template>

<script>
	import Tabbar from '@/components/tabbar.vue'
	export default {
		data() {
			return {

			}
		},
		components: {
			'view-tabbar': Tabbar
		},
		onShow() {
			uni.hideTabBar({
				animation: false
			})
		},
		methods: {

		}
	}
</script>

<style lang="scss">
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值