uni-app - - - - - 自定义tabbar

uni-app - - - - - 自定义tabbar

  • [1. 创建页面](#1. 创建页面)
  • [2. pages.json](#2. pages.json)
  • [3. 自定义tabbar](#3. 自定义tabbar)
  • [4. 隐藏原生tabbar](#4. 隐藏原生tabbar)
  • [5. 全局注册组件](#5. 全局注册组件)
  • [6. 页面使用](#6. 页面使用)
  • [7. 效果图展示](#7. 效果图展示)

1. 创建页面

2. pages.json

配置tabbar

json 复制代码
{
	"tabBar": {
		"list": [{
				"pagePath": "pages/ballroom/ballroom",
				"text": "球类房"
			},
			{
				"pagePath": "pages/gym/gym",
				"text": "健身房"

			},
			{
				"pagePath": "pages/yoga-room/yoga-room",
				"text": "瑜伽室"

			},
			{
				"pagePath": "pages/league-class/league-class",
				"text": "团课"

			},
			{
				"pagePath": "pages/my-reservation/my-reservation",
				"text": "我的预定"
			}
		]
	}
}

3. 自定义tabbar

html 复制代码
<!-- 自定义底部导航栏 -->
<template>
	<view class="tabbar-container" :style="{backgroundImage:'url('+bg+')'}">
		<!-- :style="['width: calc(100% /' + tabbar.length + ')']" -->
		<view class="tabbar-item" :class="[item.centerItem ? ' center-item' : '']" @click="changeItem(item)"
			v-for="(item, i) in tabbar" :key="i">
			<view class="item-top">
				<image :src="curItem === item.id ? item.selectedIconPath : item.iconPath" />
			</view>
			<view class="item-bottom" :class="[curItem === item.id ? 'item-active' : '']">{{ item.text }}</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			/* 当前导航栏 */
			currPage: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				curItem: 0, // 当前所选导航栏
				bg: 'https://*******/tabbar-bg.jpg',
				tabbar: [{
						id: 0,
						pagePath: "/pages/ballroom/ballroom",
						text: "球类房",
						iconPath: '/static/tabbar-icon/ball.png',
						selectedIconPath: '/static/tabbar-icon/ball-actived.png',
						centerItem: false
					},
					{
						id: 1,
						pagePath: "/pages/gym/gym",
						text: "健身房",
						iconPath: '/static/tabbar-icon/gym.png',
						selectedIconPath: '/static/tabbar-icon/gym-actived.png',
						centerItem: false
					},
					{
						id: 2,
						pagePath: "/pages/yoga-room/yoga-room",
						text: "瑜伽室",
						iconPath: '/static/tabbar-icon/yoga.png',
						selectedIconPath: '/static/tabbar-icon/yoga-actived.png',
						centerItem: false
					},
					{
						id: 3,
						pagePath: "/pages/league-class/league-class",
						text: "团课",
						iconPath: '/static/tabbar-icon/league.png',
						selectedIconPath: '/static/tabbar-icon/league-actived.png',
						centerItem: false
					},
					{
						id: 4,
						pagePath: "/pages/my-reservation/my-reservation",
						text: "我的预定",
						iconPath: '/static/tabbar-icon/my-reservation.png',
						selectedIconPath: '/static/tabbar-icon/my-reservation-actived.png',
						centerItem: false
					}
				] // 导航栏列表
			};
		},
		mounted() {
			this.curItem = this.currPage; // 当前所选导航栏
			// #ifdef H5
			uni.hideTabBar(); // 隐藏 tabBar 导航栏
			// #endif

			this.curItem = this.currPage; // 当前所选导航栏
		},
		methods: {
			/* 导航栏切换 */
			changeItem(e) {
				console.log('切换tab', e.pagePath)

				// #ifdef MP-WEIXIN
				uni.switchTab({
					url: e.pagePath,
					fail: function(error) {
						console.error("Failed to navigate:", error);
					}
				}); // 跳转到其他 tab 页面
				// #endif

				// #ifdef MP-ALIPAY
				uni.switchTab({
					url: e.pagePath,
					fail: function(error) {
						console.error("Failed to navigate:", error);
					}
				}); // 跳转到其他 tab 页面
				// #endif

			}
		}
	};
</script>

<style lang="scss" scoped>
	$textDefaultColor: #999; // 文字默认颜色
	$bottomBg: #fff; // 底部背景
	$textSelectedColor: #E51717; // 文字选中颜色
	$centerItemBg: #fff; // 中间凸起按钮背景

	.tabbar-container {
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		width: 100%;
		height: 188rpx;
		color: $textDefaultColor;
		box-shadow: 0 0 10rpx #999;
		background-size: 100% 100%;
		background-color: #ccc;
	}

	.tabbar-item {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		text-align: center;
		width: 150rpx;
		height: 120rpx;
		position: relative;

		.item-top {
			flex-shrink: 0;
			width: 150rpx;
			height: 120rpx;
			// background-color: aqua;
			// width: 65rpx;
			// height: 65rpx;
			// padding: 4rpx;

			image {
				width: 150rpx;
				height: 120rpx
			}
		}

		.item-bottom {
			width: 100%;
			font-size: 20rpx;
			position: absolute;
			bottom: 10rpx;
		}

		.item-active {
			color: $textSelectedColor;
		}
	}

	.center-item {
		position: relative;

		.item-top {
			position: absolute;
			top: -55rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 105rpx;
			height: 105rpx;
			background-color: $centerItemBg;
			border-radius: 50%;
		}

		.item-bottom {
			position: absolute;
			bottom: 5rpx;
		}
	}
</style>

4. 隐藏原生tabbar

App.vue

js 复制代码
		onLaunch: function() {
			console.log('App Launch')
			// #ifdef MP-ALIPAY
			uni.hideTabBar()
			// #endif

			// #ifdef MP-WEIXIN
			uni.hideTabBar()
			// #endif
		},

5. 全局注册组件

main.js

js 复制代码
import Vue from 'vue'
import './uni.promisify.adaptor'

6. 页面使用

html 复制代码
<template>
	<view class="common-containter">
		团课
		<custom-tabbar :curr-page="3" />
	</view>
</template>

<script>
	export default {
		data() {
			return {

			};
		}
	}
</script>

<style lang="scss">

</style>

7. 效果图展示

相关推荐
头发还在的女程序员1 天前
基于 Spring Boot + Vue 3 + uni-app 的灵活用工系统:四端架构与业务流程设计
vue.js·spring boot·uni-app·灵活用工·灵工系统
Dumbness3 天前
订阅消息弹窗必须在用户点击里调:联调实验室里 timing 和 Mock 支付通知踩的坑
javascript·uni-app
码间留白4 天前
uni-app 鸿蒙离线打包:一个改动从源码到真机要穿过几条链
uni-app·鸿蒙·deveco·离线打包·uts插件
多客:duokelijie5 天前
代练订单系统选型思路:跨品类游戏接入、订单分层管理、自动结算逻辑拆解!
数据库·mysql·微信小程序·前端框架·uni-app·php·持续部署
码间留白5 天前
鸿蒙端对接 BLE 血压计:从一次握手到读数入库(二)设备篇
物联网·uni-app·鸿蒙·蓝牙ble·血压计
华玥作者6 天前
Vue 3 + TypeScript + uni-app 组件库推荐:hy-app(华玥组件库)
vue.js·typescript·uni-app
FinelyYang6 天前
uniapp使用kkfileview在线预览ppt,excel,word,pdf类型文件
pdf·uni-app·powerpoint
2501_916008896 天前
iPhone 设备信息查看,用 KeyMob 看型号、序列号、电池健康和 UDID
android·ios·小程序·https·uni-app·iphone·webview
ModyQyW7 天前
vite-plugin-uni-manifest 0.7:在 uni-app 读取之前生成
前端·uni-app
lyz2468597 天前
uniapp vue3 小程序开发注意事项
uni-app