uniapp自定义底部tabbar
好的,您想了解如何在uniapp中自定义底部tabbar吗?可以使用uni-navbar插件来实现自定义tabbar效果。您可以通过设置tabbar的背景色、选中文字颜色、未选中文字颜色、图标等属性来自定义tabbar。另外,您也可以使用uni-icons和字体图标库来为每个选项卡添加图标。希望能对您有所帮助。
uniapp 自定义底部tabbar
创建自定义底部 TabBar
在 components
目录下建立一个新的组件文件名为 CustomTabBar.vue
来开发自定义组件[^1]。
<template>
<view class="custom-tab-bar">
<!-- 定义底部导航栏的内容 -->
<view v-for="(item, index) in tabBarList" :key="index" @click="switchPage(item.pagePath)">
<image :src="currentIndex === index ? item.selectedIconPath : item.iconPath"></image>
<text :class="{ active: currentIndex === index }">{{ item.text }}</text>
</view>
</view>
</template>
<script setup>
import { ref } from 'vue'
const props = defineProps({
current: {
type: Number,
default: 0
}
})
// 底部标签栏列表数据
const tabBarList = [
{ pagePath: '/pages/index/index', iconPath: '/static/images/home.png', selectedIconPath: '/static/images/home-active.png', text: '首页' },
{ pagePath: '/pages/category/index', iconPath: '/static/images/category.png', selectedIconPath: '/static/images/category-active.png', text: '分类' },
{ pagePath: '/pages/my/index', iconPath: '/static/images/my.png', selectedIconPath: '/static/images/my-active.png', text: '我的' }
]
let currentIndex = ref(props.current)
function switchPage(pagePath) {
uni.switchTab({ url: pagePath })
}
</script>
<style scoped>
.custom-tab-bar {
display: flex;
justify-content: space-around;
align-items: center;
height: 50px;
}
.active {
color: blue;
}
</style>
此代码展示了如何构建一个简单的自定义底部 TabBar 组件,其中包含了三个选项卡:“首页”,“分类” 和 “我的”。每个项都有未选中状态下的图标路径 (iconPath
) 和选中状态下图标路径 (selectedIconPath
)。当点击某个项目时会触发 switchPage()
方法来切换页面[^2]。
为了使这个自定义组件生效,在需要显示的地方引入并注册该组件:
<!-- BaseApp/pages/my.vue 文件中的模板部分 -->
<template>
<view>
<text class="title">{{ title }}</text>
<custom-tab-bar :current="2"></custom-tab-bar> <!-- 使用自定义组件 -->
</view>
</template>
<script setup>
import CustomTabBar from '../../components/CustomTabBar.vue'
import { ref } from 'vue'
let title = ref('我的')
</script>
这里通过 <custom-tab-bar>
标签使用了之前创建好的自定义组件,并传递了一个属性 current
表示当前激活的是第三个选项卡(索引从零开始计数),即 "我的"。
uniapp自定义底部tabbar图片
实现 UniApp 自定义底部 TabBar 的图片
为了在 UniApp 中设置自定义底部 TabBar 图片,需遵循特定步骤来确保图像能够正确显示并响应不同状态。
创建 CustomTabBar 组件
首先,在 components
目录下创建名为 CustomTabBar.vue
的新组件文件[^1]。此文件用于构建自定义的 TabBar 结构及其样式逻辑:
<template>
<view class="custom-tab-bar">
<!-- 循环渲染每个 tabItem -->
<block v-for="(item, index) in tabBarList" :key="index">
<view
@click="switchTab(item.pagePath)"
:class="[currentIndex === index ? 'active' : '', 'tab-item']"
>
<image :src="currentIndex === index ? item.selectedIconPath : item.iconPath"></image>
<text>{{ item.text }}</text>
</view>
</block>
</view>
</template>
<script>
export default {
data() {
return {
currentIndex: 0,
tabBarList: [
{ pagePath: '/pages/index/index', text: '首页', iconPath: '/static/images/home.png', selectedIconPath: '/static/images/home-active.png'},
{ pagePath: '/pages/category/index', text: '分类', iconPath: '/static/images/category.png', selectedIconPath: '/static/images/category-active.png'},
{ pagePath: '/pages/cart/index', text: '购物车', iconPath: '/static/images/cart.png', selectedIconPath: '/static/images/cart-active.png'},
{ pagePath: '/pages/user/index', text: '我的', iconPath: '/static/images/mine.png', selectedIconPath: '/static/images/mine-active.png'}
]
}
},
methods: {
switchTab(url){
uni.switchTab({url})
}
}
}
</script>
<style scoped>
.custom-tab-bar{
display: flex;
justify-content: space-around;
align-items: center;
height: 50px;
}
.tab-item image{
width: 24px; /* 设置图标宽度 */
height: 24px; /* 设置图标高度 */
}
.active{
color: red !important; /* 当前选中的颜色 */
}
</style>
上述代码展示了如何通过循环遍历 tabBarList
数组来自动生成各个标签项,并根据不同条件加载对应的默认或激活状态下图片资源路径。
注册全局组件
接着,在项目的入口文件 main.js
中引入已编写完成的 CustomTabBar
组件,并将其注册为全局可用组件以便于在整个应用内调用[^2]:
import tabbar from '@/components/CustomTabBar/CustomTabBar.vue'
Vue.component('custom-tab-bar', tabbar);
这样就完成了基本配置工作,使得可以在任何页面中直接使用 <custom-tab-bar></custom-tab-bar>
来展示带有自定义图标的底部导航栏。
相关推荐
















