当前位置: 首页 > article >正文

uniapp小程序自定义中间凸起样式底部tabbar

我自己写的自定义的tabbar效果图自定义tabbar效果图
废话少说咱们直接上代码,一步一步来

第一步:

找到根目录下的 pages.json 文件,在 tabBar 中把 custom 设置为 true,默认值是 false。list 中设置自定义的相关信息,

pagePath: 页面路径;
iconPath: 点击前显示的图片;
selectedIconPath: 点击后显示的图片;
text: 底部导航的名称。
在这里插入图片描述

第二步

在根目录下创建 components 文件夹用来存放自定义tabBar组件,小伙伴们可以自定义文件夹和文件的名称。记得命名要规范哦!!!
在这里插入图片描述

第三步

咱也不截图了,看了那么多的文章全他妈截图,咱直接上代码方便有需要的小伙伴使用!!!!

<template>
	<view class="tabbar-container flex items-center">
		<!-- isRound是中间凸出样式的标志,用来判断当前子级是否是凸出样式的 -->
		<view :class="!item.isRound ? 'square' : 'is-square'" v-for="(item, index) in tabbarList" :key="index" @click="tabbarChange(index, item)">
			<!-- 不是凸出的子级 -->
			<template v-if="!item.isRound">
				<view class="item-top flex justify-center flex-col">
					<u-image :src="active == index ? item.selectedIconPath : item.iconPath" :width="item.width"
						:height="item.height"></u-image>
				</view>
				<view class="item-bottom" :class="{'active': active === index}">
					<text>{{ item.text }}</text>
				</view>
			</template>
			<!-- 凸出的子级 -->
			<template v-else>
				<view class="flex flex-col justify-center items-center center-round">
					<view class="flex flex-col justify-center items-center">
						<u-image :src="active == index ? item.selectedIconPath : item.iconPath" :width="item.width"
							:height="item.height"></u-image>
						<text :class="{'round-active': active === index}">{{ item.text }}</text>
					</view>
				</view>
			</template>
		</view>
	</view>
</template>
<script>
	export default {
		name: 'Tabbar',
		props: {
			tabbarIndex: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				// tabbar列表,配置自定义用到的属性字段
				tabbarList: [{
						pagePath: "/pages/homePage/index",
						iconPath: "/static/images/tabbar/index.png",
						selectedIconPath: "/static/images/tabbar/index-selected.png",
						text: "首页",
						width: '45rpx',
						height: '41rpx',
						isRound: false
					},
					{
						pagePath: "/pages/information/index",
						iconPath: "/static/images/tabbar/data.png",
						selectedIconPath: "/static/images/tabbar/data-selected.png",
						text: "数据",
						width: '44rpx',
						height: '43rpx',
						isRound: false
					},
					{
						pagePath: "/pages/aiRecommend/index",
						iconPath: "/static/images/tabbar/ai-recommend.png",
						selectedIconPath: "/static/images/tabbar/ai-recommend.png",
						text: "智能推荐",
						width: '120rpx',
						height: '120rpx',
						isRound: true
					},
					{
						pagePath: "/pages/vip/index",
						iconPath: "/static/images/tabbar/vip.png",
						selectedIconPath: "/static/images/tabbar/vip-selected.png",
						text: "会员",
						width: '39rpx',
						height: '37rpx',
						isRound: false
					},
					{
						pagePath: "/pages/center/index",
						iconPath: "/static/images/tabbar/center.png",
						selectedIconPath: "/static/images/tabbar/center-selected.png",
						text: "我的",
						width: '40rpx',
						height: '41rpx',
						isRound: false
					},
				],
				active: 0,
				isRound: false,
			}
		},
		mounted() {
			this.active = this.tabbarIndex
		},
		methods: {
			tabbarChange(index, item) {
				this.$emit('updateTabbar', index)
				uni.switchTab({
					url: item.pagePath
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tabbar-container {
		width: 100%;
		height: 132rpx;
		position: fixed;
		bottom: 0;
		.square {
			width: 100%;
			height: 130rpx;
			background: #FFFFFF;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			.round-active {
				color: #207BDB;
			}
		}
		.square .item-top {
			width: 45rpx;
			height: 45rpx;
		}
		.is-square {
			width: 100%;
			height: 130rpx;
			background: #FFFFFF;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			.round-active {
				color: #207BDB;
			}
		}
		.center-round {
			width: 203rpx;
			height: 130rpx;
			background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMsAAAAlCAYAAADstR3BAAADx0lEQVR4nO3deajnUxjH8dc1Y2zDzFjGvkZkKcofpNGgTCl/oGRfyto0iJJQypa1KJT9H7KUJYSsDRFlGqQUyi4kDVe2y1wdnqmZ3HSv+1vOOd/z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hGYzr8EOs1S0M4ae3t136OaBLLvNV+H52iWufGavqqVzrIYPO4LzEJZJdocqz9UO3G8EnP7Qfx+iju3fwyZtbSc51qoDR5kN6nm9EmS3quZ2LdvwC4r+EYTWev0AAAAABJRU5ErkJggg==');
			background-position: center top;
			background-size: 100% 37rpx;
			background-repeat: no-repeat;
			position: absolute;
			margin-top: -72rpx;

			text {
				font-weight: 500;
				font-size: 20rpx;
				color: #555B66;
				position: absolute;
				top: 117rpx;
			}
		}

		.item-bottom {
			font-weight: 500;
			font-size: 20rpx;
			color: #555B66;
			margin-top: 14rpx;
		}

		.active {
			color: #207BDB;
		}
	}
</style>

注意:小程序背景图无法使用本地图片,要么转成base64,要么把图片存到服务器上,然后调用接口把图片路径返回。推荐第二种方法。

我写的有点繁琐,中间凸起的样式应该为一整块,可是ui只给了顶部的白色图片导致写的代码有点多。不过大体的思路差不多都是这样,小伙伴们也可以参考其他博主写的文章的思路去编写自定义组件。
在这里插入图片描述
这是我用到的图片,小伙伴们可以下载下来配合代码进行使用。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第四步

在需要的页面中引用自定义组件

<template>
	<Tabbar :tabbar-index="tabbarIndex" @updateTabbar="updateTabbar"></Tabbar>
</template>
<script>
	import Tabbar from "@/components/tabbar/tabbar.vue"
	components: { Tabbar },
	data() {
		return {
			tabbarIndex: 4,
		}
	},
	methods: {
		updateTabbar(e) {
			this.tabbarIndex = e
		},
	}
</script>

有需求的小伙伴们可以试试,我写的并不是很完美但是大体的思路是这样的,小伙伴们可以根据自己项目的需求进行改动。此文章可作为参考使用,希望能帮到有需求的小伙伴!!!!

如果有用的话就点击收藏起来吧!!!


http://www.kler.cn/a/534545.html

相关文章:

  • 3.5 Go(特殊函数)
  • Linux——基础命令1
  • Python爬虫-如何正确解决起点中文网的无限debugger
  • InnoDB和MyISAM的比较、水平切分和垂直切分、主从复制中涉及的三个线程、主从同步的延迟产生和解决
  • [c语言日寄]赋值操作对内存的影响
  • 【C++】STL——list底层实现
  • 记录使用libvirt创建虚拟机、自定义qcow2镜像
  • 图像分割中根据mask的ROI,去除mask和image中没有勾画ROI层数以外的图像
  • 数据库技术基础
  • Certum OV企业型通配符SSL
  • 常用工具类——Collections集合框架
  • c++ 基础 计算机的内存和寻址机制
  • Redis面试题总结(题目来源JavaGuide)
  • LeetCode 3442.奇偶频次间的最大差值 I
  • ASP.NET Core筛选器Filter
  • Vue3.5常用特性整理
  • 一、tsp学习笔记——开发环境搭建
  • 计算机网络笔记再战——理解几个经典的协议6——TCP与UDP
  • Mysql-增删改查(知识点总结)
  • WPS的word文档加密
  • module ‘matplotlib.cm‘ has no attribute ‘get_cmap‘
  • 在https下引用IC卡读卡器web插件
  • LeetCode 257.二叉树的所有路径
  • BUU10 [极客大挑战 2019]LoveSQL1
  • RK3576——USB3.2 OTG无法识别到USB设备
  • docker容器编排工具 docker compose