uniApp自定义tabBar底部导航栏+实现tabBar中间凸起自定义样式

一.配置信息

pages.js 添加 "custom": true 属性

二. 定义TabBar组件

<template>
	<view class="tabbar-container">
		<view v-for="(item,index) in tabbarList" :class="[item.centerItem && 'center-item'  ,'tabbar-item']"
			@click="changeItem(item)" :key="item.id">
			<view class="item-top">
				<image v-if="item.id!==2" :src="activeTab===item.path?item.selectIcon:item.icon"
					:class="[activeTab===item.path&&'active']"></image>
				<text v-else class="iconfont icon-add"></text>
			</view>
			<view :class="[activeTab===item.path ? 'item-active' : '','item-bottom']">
				<text class="text">{{item.text}}</text>
			</view>
		</view>

		<u-action-sheet class="action-sheet" round="20" :show="sheetShow" title="添加" @close="handleClose">
			<view class="gutter">

			</view>
		</u-action-sheet>
	</view>
</template>

<script>
	import {
		mapGetters
	} from "vuex"
	export default {
		data() {
			return {
				currentItem: 0,
				tabbarList: [{
					id: 0,
					path: "/pages/home/index",
					icon: "/static/img/home.png",
					selectIcon: "/static/img/home_fill.png",
					text: "首页",
					centerItem: false
				}, {
					id: 1,
					path: "/pages/monitor/index",
					icon: "/static/img/playon.png",
					selectIcon: "/static/img/playon_fill.png",
					text: "监控",
					centerItem: false
				}, {
					id: 2,
					icon: "/static/img/playon.png",
					selectIcon: "/static/img/playon_fill.png",
					text: "",
					centerItem: true
				}, {
					id: 3,
					path: "/pages/notice/index",
					icon: "/static/img/message.png",
					selectIcon: "/static/img/message_fill.png",
					text: "通知",
					centerItem: false
				}, {
					id: 4,
					path: "/pages/member/index",
					icon: "/static/img/mine.png",
					selectIcon: "/static/img/mine_fill.png",
					text: "我的",
					centerItem: false
				}],
				sheetShow: false,
			};
		},
		computed: mapGetters(['activeTab']),
		created() {
			let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
			let curRoute = routes[routes.length - 1].route //获取当前页面路由
			this.$store.commit('setActiveTab', '/' + curRoute);
		},
		mounted() {
			uni.hideTabBar();
		},
		methods: {
			changeItem(item) {
				if (item?.path) {
					var pages = getCurrentPages();
					var page = pages[pages.length - 1].route;
					if (item.path == '/' + page) return
					this.$store.commit('setActiveTab', item.path);
					uni.switchTab({
						url: item.path,
					})
				} else {
					this.sheetShow = !this.sheetShow;
				}
			},

			//关闭快捷操作
			handleClose() {
				this.sheetShow = false;
			},
		},
	}
</script>
<style lang="scss" scoped>
	.tabbar-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 100rpx;
		// box-shadow: 0px 3px 20px rgba(0, 0, 0, 0.16);
		border-top: 4rpx solid #e4e4e4;
		display: flex;
		align-items: center;
		color: #999999;
		z-index: 200;
		background-color: #fff;


		.tabbar-item {
			width: 20%;
			height: 100%;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			align-items: center;
			text-align: center;

			.item-top {
				width: 50rpx;
				height: 50rpx;

				>image {
					width: 100%;
					height: 100%;
				}

				.active {
					animation: mymove 1s 1;
				}

				@keyframes mymove {
					0% {
						transform: scale(1);
						/*开始为原始大小*/
					}

					10% {
						transform: scale(0.8);
					}

					30% {
						transform: scale(1.1);
						/*放大1.1倍*/
					}

					50% {
						transform: scale(0.9);
						/*放大1.1倍*/
					}

					70% {
						transform: scale(1.05);
					}

					90% {
						transform: scale(1);
					}
				}
			}

			.item-bottom {
				font-size: 26rpx;
				width: 100%;
			}
		}

		.center-item {
			display: block;
			position: relative;

			.item-top {
				flex-shrink: 0;
				width: 100rpx;
				height: 100rpx;
				position: absolute;
				top: -50rpx;
				left: calc(50% - 50rpx);
				border-radius: 50%;
				box-shadow: 0px 3px 20px rgba(0, 0, 0, 0.16);
				background-color: #ff6a00;

				.iconfont {
					position: absolute;
					left: 50%;
					top: 50%;
					transform: translate(-50%, -50%);
					font-size: 70rpx;
					font-weight: 500;
					color: #fffefd;
				}
			}

			.item-bottom {
				position: absolute;
				bottom: 5rpx;
			}
		}

		.item-active {
			color: #ff6a00;
		}


		.action-sheet {
			.gutter {
				display: flex;
				margin-top: 20rpx;
				margin-bottom: 40rpx;

				.item {
					flex: 1;

					&:last-child {
						margin-right: 0;
					}

					.iconfont {
						font-size: 56rpx;
					}

					.yellow {
						color: #F1C40F;
					}

					.purple {
						color: #9B59B6;
					}

					.turquoise {
						color: #1ABC9C;
					}

					.disabled {
						color: #ccc;
					}
				}
			}
		}
	}
</style>

三. 把tabbar url地址存在vuex

export default {
	state: {
		activeTab: "", // 底部tabbar url地址
	},
	mutations: {
		setActiveTab(state, url) {
			state.activeTab = url;
		},
	},
	getters: {
		activeTab: state => state.activeTab,
	}
}

四. 在页面引入

<template>
	<view>
		我的
		<TabBar />
	</view>
</template>

<script>
	import TabBar from '@/components/Tabbar/Tabbar.vue'
	export default {
		components: {
			TabBar,
		},
	}
</script>

效果图

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过自定义底部 Tabbar实现凸起透明底部的效果。下面是一种实现方式: 1. 在 `uniapp` 项目的 `static` 目录下创建一个 `custom-tabbar` 文件夹,用于存放自定义 Tabbar 的相关资源。 2. 在 `custom-tabbar` 文件夹中创建一个 `tabbar.vue` 组件文件,作为自定义 Tabbar 的模板。在模板中,你可以使用 `<template>` 标签定义底部 Tabbar 的布局。 3. 在 `tabbar.vue` 组件中,使用 `<slot>` 标签创建插槽,用于显示外部传入的 Tabbar 内容。同时,在该标签上添加样式,使其能够凸起显示。 ```html <template> <div class="custom-tabbar"> <slot></slot> </div> </template> <style scoped> .custom-tabbar { position: fixed; bottom: 0; left: 0; right: 0; background-color: transparent; border-top: 1px solid #ccc; display: flex; flex-direction: row; } </style> ``` 4. 在需要使用自定义 Tabbar 的页面中引入 `custom-tabbar` 组件,并在该组件内部插入具体的 Tabbar 内容。 ```html <template> <div> <!-- 此处为页面内容 --> ... <!-- 引入自定义 Tabbar --> <custom-tabbar> <!-- 插入具体的 Tabbar 内容 --> <view class="tabbar-item">首页</view> <view class="tabbar-item">消息</view> <view class="tabbar-item">我的</view> </custom-tabbar> </div> </template> <style> .tabbar-item { flex: 1; padding: 10px; text-align: center; } </style> ``` 5. 根据实际需求,你可以在 Tabbar 的模板中添加更多的样式和交互逻辑。 这样,你就可以实现一个自定义底部 Tabbar,使其能够凸起并具有透明底部的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值