更新记录
1.0.2(2025-04-30) 下载此版本
优化说明md
1.0.1(2025-04-30) 下载此版本
解决使用中遇到的问题,优化组件
1.0.0(2024-11-22) 下载此版本
组件新发布
查看更多平台兼容性
Vue2 | Vue3 |
---|---|
√ | √ |
App | 快应用 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节小程序 | QQ小程序 |
---|---|---|---|---|---|---|
HBuilderX 3.1.0 | × | √ | √ | × | × | √ |
钉钉小程序 | 快手小程序 | 飞书小程序 | 京东小程序 | 鸿蒙元服务 |
---|---|---|---|---|
× | × | × | × | × |
H5-Safari | Android Browser | 微信浏览器(Android) | QQ浏览器(Android) | Chrome | IE | Edge | Firefox | PC-Safari |
---|---|---|---|---|---|---|---|---|
√ | √ | √ | √ | × | × | × | × | × |
hbxw-tabbar自定义tabbar组件
介绍
hbxw-tabbar自定义tabbar组件 当原生tabbar无法满足产品需求的时候我们不得不自定义tabbar,像需要根据不同用户显示不同的tabbar,中间突出的怪异tabbar,那此组件可能适合你。
使用示例
使用前要以新建一个页面拷贝下面代码做一下测试,熟悉后再使用到项目中
<template>
<view class="conatainer">
<button @click="tabbarToggle('normal')" :style="{color: tabbarType === 'normal' ? 'green' : '#333'}">标准款式</button>
<button @click="tabbarToggle('coustom')" :style="{color: tabbarType === 'coustom' ? 'green' : '#333'}">自定义款式</button>
<!-- 标准使用 -->
<hbxw-tabbar
v-if="tabbarType === 'normal'"
:config="tabbar"
activeKey="tabbar0"
></hbxw-tabbar>
<!-- 通过插槽自定义tabbar -->
<hbxw-tabbar
v-if="tabbarType === 'coustom'"
:config="tabbar"
activeKey="tabbar2"
>
<template #default="{item, config, index, isActive, bgColor, iconStyleIn, activeIconStyleIn, textStyleIn, activeTextStyleIn, tagStyle}">
<view class="hbxw-tabbar-item" :style="{height: config.tabbarHeight + 'rpx'}" v-if="item.key !== 'tabbar2'">
<view class="hbxw-tabbar-itemin" :class="{'hbxw-tabbar-active': isActive}">
<view class="hbxw-tabbar-icon" :style="isActive ? iconStyleIn : activeIconStyleIn">
<image
class="hbxw-tabbar-image"
:src="isActive ? item.selectedIconPath : item.iconPath "
/>
<view class="hbxw-tabbar-tag" :style="{tagStyle}" v-if="item.tag">{{typeof item.tag !== 'boolean' ? item.tag : ''}}</view>
</view>
<text class="hbxw-tabbar-text" :style="isActive ? activeTextStyleIn : textStyleIn">{{item.text}}</text>
</view>
</view>
<view v-else class="custom-tabbar-item" :style="{'border-style': bgColor, 'background-color': bgColor}">
<view class="custom-tabbar-item-in">
<image class="custom-tabbar-icon" :src="isActive ? item.selectedIconPath : item.iconPath" />
<view class="hbxw-tabbar-tag" :style="{tagStyle}" v-if="item.tag">{{typeof item.tag !== 'boolean' ? item.tag : ''}}</view>
</view>
</view>
</template>
</hbxw-tabbar>
</view>
</template>
<script>
export default {
data() {
return {
tabbarType: 'normal',
// tabbar配置
tabbar: {
"color": "#8a8a8a",
"selectedColor": "#1f1f1f",
"borderStyle": "white",
"backgroundColor": "#ffffff",
"tabbarHeight": 122,
"holderHeight": 142,
"iconStyle": {"width": '60rpx', "height": '60rpx'},
"activeIconStyle": {"width": '60rpx', "height": '60rpx'},
"textStyle": {"fontSize": '24rpx'},
"activeTextStyle": {"fontSize": '24rpx'},
"list": [
{
"pagePath": "pages/home/home",
"iconPath": "/static/hbxwtabbar/home.png",
"selectedIconPath": "/static/hbxwtabbar/home_selected.png",
"text": "首页",
"key": "tabbar0"
},
{
"pagePath" : "pages/equity/index",
"iconPath" : "/static/hbxwtabbar/goods.png",
"selectedIconPath" : "/static/hbxwtabbar/goods_selected.png",
"text" : "商品",
"key": "tabbar1",
"tag": 10
},
{
"pagePath" : "pages/rentAll/index",
"iconPath" : "/static/hbxwtabbar/activity.png",
"selectedIconPath" : "/static/hbxwtabbar/activity_selected.png",
"text" : "活动",
"key": "tabbar2",
"tag": true
},
{
"pagePath" : "pages/kefu/index",
"iconPath" : "/static/hbxwtabbar/customer_service.png",
"selectedIconPath" : "/static/hbxwtabbar/customer_service_selected.png",
"text" : "客服",
"key": "tabbar3"
},
{
"pagePath": "pages/me/me",
"iconPath": "/static/hbxwtabbar/mine.png",
"selectedIconPath": "/static/hbxwtabbar/mine_selected.png",
"text": "我的",
"key": "tabbar4"
}
]
}
}
},
methods: {
tabbarToggle(type){
this.tabbarType = type
}
}
}
</script>
<style lang="scss">
.conatainer{
height: 150vh;
background-color: green;
}
.hbxw-tabbar-item{
flex:1;
height: 100%;
display: flex;
flex-direction:row;
justify-content: center;
align-items: center;
}
.hbxw-tabbar-itemin{
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
&.hbxw-tabbar-active{
.hbxw-tabbar-text{
color: red;
}
}
}
.hbxw-tabbar-icon{
width: 40rpx;
height: 40rpx;
position: relative;
}
.hbxw-tabbar-tag{
position: absolute;
top: -10rpx;
right: -10rpx;
display: flex;
justify-content: center;
align-items: center;
min-width:20rpx;
padding: 0 5rpx;
box-sizing: border-box;
height: 20rpx;
border-radius: 10rpx;
overflow: hidden;
line-height: 1em;
font-size: 10px;
background-color: red;
color: #fff;
}
.hbxw-tabbar-image{
width: 100%;
height: 100%;
}
.hbxw-tabbar-text{
font-size: 18rpx;
color: #999;
}
/* 自定义tabbar项 */
.custom-tabbar-item{
width: 142rpx;
height: 142rpx;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
border-radius: 50%;
border: 10rpx solid white;
background-color: white;
box-sizing: border-box;
}
.custom-tabbar-item-in{
width: 84rpx;
height: 84rpx;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
position: relative;
}
.custom-tabbar-icon{
width: 100%;
height: 100%;
}
</style>
组件属性
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
config | Object | null | tabbar配置对象 |
activeKey | String | '' | 当前高亮项的key |
customKey | String | '' | 自定义视图项的key |
tabbarHeight | Number | 0 | tabbar高度(rpx) |
holderHeight | Number | 0 | 占位高度(rpx) |
tabbarStyle | Object | null | tabbar样式 |
iconStyle | Object | null | 图标样式 |
activeIconStyle | Object | null | 选中时图标样式 |
textStyle | Object | null | 文字样式 |
activeTextStyle | Object | null | 选中时文字样式 |
tagStyle | Object | null | 角标样式 |
config配置项说明
{
"color": "#8a8a8a", // 文字默认颜色
"selectedColor": "#1f1f1f", // 文字选中颜色
"borderStyle": "white", // 边框颜色
"backgroundColor": "#ffffff", // 背景颜色
"tabbarHeight": 122, // tabbar内容高度
"holderHeight": 142, // tabbar占位高度,正常是和tabbarHeight高度是一样的,用于异形突出tabbar,控制tabbar占位高度,防止挡住页面内容
"iconStyle": {"width": '60rpx', "height": "60rpx"}, // 图标样式自定义
"activeIconStyle": {"width": "60rpx", "height": "60rpx"}, // 选中状态图片样式自定义
"textStyle": {"fontSize": "24rpx"}, // 文字样式自定义
"activeTextStyle": {"fontSize": "24rpx"}, // 选中状态下的文字样式自定义
"tagStyle": {"color": "#666"}
"list": [ // tabbar项列表
{
"pagePath": "pages/home/home", // 页面路径
"iconPath": "/static/hbxwtabbar/home.png", // 默认图标
"selectedIconPath": "/static/hbxwtabbar/home_selected.png", // 选中图标
"text": "首页", // 文字
"key": "tabbar0", // 唯一标识
"tag": "99" // 角标文本,true显示红点,false不显示,如果为数字或者字符串则显示tag显示内容
}
]
}
使用注意事项:
自定义tabbar使用的跳转方式是uni.switchTab,所以自定义tabbar一样需要先配置好原生tabbar的配置,在指定tabbar的页面onShow生命周期中调用uni.hideTabBar隐藏原生tabbar,这样做的好处的是可以无缝使用原生API
onShow(() => {
uni.hideTabBar();
})