当前位置: 代码网 > it编程>编程语言>Javascript > 详细介绍uniapp小程序自定义底部tabbar样式的方法

详细介绍uniapp小程序自定义底部tabbar样式的方法

2024年08月01日 Javascript 我要评论
原生tabBar是相对固定的配置方式,但是默认的样式可能无法满足我们的需求,所以我们需要自定义设置tabbar样式。

uniapp自带的tabbar组件可以方便地实现底部导航栏的功能,原生tabbar是相对固定的配置方式,但是默认的样式可能无法满足我们的需求,所以我们需要自定义设置tabbar样式。下面我会详细介绍uniapp自定义tabbar样式的方法。

一、pages.json代码

pages.json这里只配置页面路径就可以。

"tabbar": {
		
		"color": "#7a7e83",
		"selectedcolor": "#086d5b",
		"backgroundcolor": "#ffffff",
		"list": [
			{
				"pagepath": "pages/xxx/xxx"
			},
			{
				"pagepath": "pages/xxx/xxx"
			},
			{
				"pagepath": "pages/xxx/xxx"
			}
		]
		
	},

二、创建一个tabbar.vue组件

在uniapp项目的components目录下创建一个名为tabbar的组件,该组件包含了tabbar的整体布局和动态切换效果。

tabbar.vue组件html部分代码如下:

<template>
	<view class="tabbar" >
	    <view class="tabbar-item" v-for="(item,index) in list" :key="index"         
         @click="changetab(index)">
			<view class="select"  v-if="current == index">
				<view class="i-t position">
					<image class="img imgactive" mode="widthfix" 
                        :src="item.selectediconpath" v-if="current == index"></image>
					<image class="img" mode="widthfix" :src="item.iconpath" v-else></image>
					<view class="text active" v-if="current == index">{{item.text}}</view>
					<view class="text" v-else>{{item.text}}</view>
				</view>
			</view>
			<view v-else>
				<image class="img" mode="widthfix" :src="item.selectediconpath" 
                    v-if="current == index"></image>
				<image class="img" mode="widthfix" :src="item.iconpath" v-else></image>
				<view class="text active" v-if="current == index">{{item.text}}</view>
				<view class="text" v-else>{{item.text}}</view>
			</view>
	    </view>
	  </view>
</template>

tabbar.vue组件js部分代码如下:

<script>
    export default {
    
        name:"tabbar",
		props: ['current'],
        data() {
			return {
                list:  [
				          {
				          	pagepath: "页面路径",
				          	iconpath: "图标路径",
				          	selectediconpath: "选中的图标路径",
				          	text: "文字"
				          },
				          {
				          	pagepath: "页面路径",
				          	iconpath: "图标路径",
				          	selectediconpath: "选中的图标路径",
				          	text: "文字"
				          },
				          {
				          	pagepath: "页面路径",
				          	iconpath: "图标路径",
				          	selectediconpath: "选中的图标路径",
				          	text: "文字"
				          }
				        ]

            }
        },

        created() {
		      uni.hidetabbar() 
		    },
			
		 methods: {
		      changetab(e) {
		        uni.switchtab({
		          url: '/' + this.list[e].pagepath,
		        })
		      }
		    }


}

</script>

tabbar.vue组件css部分代码如下:

<style>
 .tabbar {
	font-size: 1.5vh;
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 99;
    width: 100%;
    height: 6vh;
    display: flex;
    align-items: center;
    justify-content: space-around;
	background-color: #fff;
    padding: 20rpx 0;
  },
  .tabbar-item {
	height: 100%;
    padding: 0 40rpx;
	display: flex;
	align-items: center;
	justify-content: center;
  }
  .img {
    height: 3vh;
    width: 2.5vh;
	margin: 0 4vw;
  }
  .text {
	text-align: center;
    color: #cacaca;
  }
  .i-t{
	  font-size: 1.5vh;
	  padding:2vw 2vh;
	  position: absolute;
	  bottom: 1vh;
  }
  .select{
	width: 10vh;
	height:10vh;
	border-radius: 10vh;
	background-color: #086d5b;
	margin-bottom: 4vh;
	position: relative;
  }
  .imgactive{
	 height: 3.5vh;
	 width: 3.2vh;
	 margin: 0 2.2vw;
  }
  .text.active {
    color: #fff;
  }
</style>

css部分样式可以根据项目需要自行修改

三、在需要显示tabbar的页面中引入tabbar组件

<template>
	<view>
		<tabbar current="0"></tabbar>
	</view>
</template>

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

以上就是uniapp小程序自定义底部tabbar样式的方法的详细内容啦·~

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com