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. 效果图展示

相关推荐
小汤猿人类13 小时前
uniapp媒体
uni-app·媒体
__不靠谱先生17 小时前
uniapp uni-table合并单元格
uni-app
爱吃玉米的螃蟹17 小时前
uniapp对tabbar封装,简单好用
uni-app
十一吖i19 小时前
uniapp实现下拉刷新
linux·服务器·uni-app
AdSet聚合广告平台21 小时前
Android app广告变现广告预算来源有哪些?
大数据·搜索引擎·ios·小程序·uni-app
程序者王大川1 天前
【移动端】Flutter与uni-app:全方位对比分析
flutter·uni-app·app·nodejs·全栈·dart·移动端
想把星星变成糖1 天前
uniapp解决页面跳转时,含有base64的数据丢失问题
uni-app
京城五2 天前
uniapp中scroll-view标签
前端·css·uni-app
某公司摸鱼前端2 天前
html 中如何使用 uniapp 的部分方法
前端·uni-app·html
小鼠米奇2 天前
uniapp如何监听页面滚动?
前端·uni-app