TnTabbar 底部导航栏
该组件一般用于自定义底部导航栏
使用自定义底部导航栏可以让页面更加酷炫,脱离原声底部导航栏的限制,但是对应的性能上或多或少会受影响
提示
建议底部导航 Item 数量为 2 到 5 个之间
组件位置
typescript
import TnTabbar from '@tuniao/tnui-vue3-uniapp/components/tabbar/src/tabbar.vue'
import TnTabbarItem from '@tuniao/tnui-vue3-uniapp/components/tabbar/src/tabbar-item.vue'
import TnTabbar from '@tuniao/tnui-vue3-uniapp/components/tabbar/src/tabbar.vue'
import TnTabbarItem from '@tuniao/tnui-vue3-uniapp/components/tabbar/src/tabbar-item.vue'
1
2
2
typescript
import TnTabbar from '@/uni_modules/tuniaoui-vue3/components/tabbar/src/tabbar.vue'
import TnTabbarItem from '@/uni_modules/tuniaoui-vue3/components/tabbar/src/tabbar-item.vue'
import TnTabbar from '@/uni_modules/tuniaoui-vue3/components/tabbar/src/tabbar.vue'
import TnTabbarItem from '@/uni_modules/tuniaoui-vue3/components/tabbar/src/tabbar-item.vue'
1
2
2
平台差异说明
App(vue) | H5 | 微信小程序 | 支付宝小程序 | ... |
---|---|---|---|---|
√ | √ | √ | √ | 适配中 |
基础使用
- 通过
TnTabbar
中的v-model
绑定当前选中TnTabbarItem
的索引值如果有设置TnTabbarItem
的name
属性则返回name
的值 - 设置
fixed
为true
可以将导航栏固定在底部 - 通过
TnTabbarItem
的icon
设置默认显示的icon
- 通过
TnTabbarItem
的active-icon
设置选中时显示的icon
- 通过
TnTabbarItem
的text
设置默认显示的文字
注意
如果设置了TnTabbarItem
的name
属性,请确保每个TnTabbarItem
的name
属性值都不一样,否则会导致v-model
绑定的值不准确
vue
<script lang="ts" setup>
import { ref } from 'vue'
const currentTabbar = ref(0)
// 导航栏数据
const tabbarData = [
{
name: '首页',
icon: 'home',
activeIcon: 'home-fill',
},
{
name: '爱好',
icon: 'like',
activeIcon: 'like-fill',
},
{
name: '积分',
icon: 'shop',
activeIcon: 'shop-fill',
},
{
name: '热谤',
icon: 'fire',
activeIcon: 'fire-fill',
},
{
name: '我的',
icon: 'my-circle',
activeIcon: 'my-circle-fill',
},
]
</script>
<template>
<TnTabbar v-model="currentTabbar" fixed>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
/>
</TnTabbar>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const currentTabbar = ref(0)
// 导航栏数据
const tabbarData = [
{
name: '首页',
icon: 'home',
activeIcon: 'home-fill',
},
{
name: '爱好',
icon: 'like',
activeIcon: 'like-fill',
},
{
name: '积分',
icon: 'shop',
activeIcon: 'shop-fill',
},
{
name: '热谤',
icon: 'fire',
activeIcon: 'fire-fill',
},
{
name: '我的',
icon: 'my-circle',
activeIcon: 'my-circle-fill',
},
]
</script>
<template>
<TnTabbar v-model="currentTabbar" fixed>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
/>
</TnTabbar>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
自定义导航栏样式
- 通过
TnTabbar
中的height
设置导航栏的高度 - 通过
TnTabbar
中的active-color
设置选中时的颜色,如果单独设置了TnTabbarItem
的active-color
则会覆盖TnTabbar
中的active-color
- 通过
TnTabbar
中的inactive-color
设置未选中时的颜色,如果单独设置了TnTabbarItem
的inactive-color
则会覆盖TnTabbar
中的inactive-color
- 通过
TnTabbar
中的icon-size
设置图标的大小,如果单独设置了TnTabbarItem
的icon-size
则会覆盖TnTabbar
中的icon-size
- 通过
TnTabbar
中的font-size
设置文字的大小,如果单独设置了TnTabbarItem
的font-size
则会覆盖TnTabbar
中的font-size
将TnTabbar
中top-shadow
设置为false
可以隐藏顶部的阴影
设置TnTabbar
的bg-color
可以修改导航栏的背景颜色,可以传递hex
、rgb
、rgba
和图鸟内置颜色的值背景颜色,需要注意如果设置frosted
为true
时开启了毛玻璃效果,那么bg-color
将只能传递rgba
的色值
vue
<template>
<TnTabbar
active-color="tn-yellow"
inactive-color="rgba(1, 190, 250, 0.3)"
height="140rpx"
icon-size="48"
font-size="34"
>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
/>
</TnTabbar>
</template>
<template>
<TnTabbar
active-color="tn-yellow"
inactive-color="rgba(1, 190, 250, 0.3)"
height="140rpx"
icon-size="48"
font-size="34"
>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
/>
</TnTabbar>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
设置角标
角标仅能在非凸起按钮上设置
- 通过设置
TnTabbarItem
的badge
可以设置角标的内容 - 通过设置
TnTabbarItem
的badge-config
可以配置角标的属性
显示带内容的角标
vue
<template>
<TnTabbar>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
badge="99"
/>
</TnTabbar>
</template>
<template>
<TnTabbar>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
badge="99"
/>
</TnTabbar>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
显示点角标
vue
<template>
<TnTabbar>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
badge="99"
:badge-config="{
dot: true,
}"
/>
</TnTabbar>
</template>
<template>
<TnTabbar>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
badge="99"
:badge-config="{
dot: true,
}"
/>
</TnTabbar>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
设置 TabbarItem 突出显示
不建议动态设置 TabbarItem 突出显示,建议将中间的突出显示
通过设置TnTabbarItem
的bulge
为true
可以将对应的TnTabbarItem
突出显示
- 通过
TnTabbarItem
的bulge-bg-color
可以单独设置凸起按钮激活时的背景,如果不设置默认使用active-color
- 通过
TnTabbarItem
的bulge-text-color
可以单独设置凸起按钮激活时的文字颜色
vue
<template>
<TnTabbar v-model="currentTabbar" fixed>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
:bulge="index === 2"
bulge-bg-color="tn-gradient__cool-6"
/>
</TnTabbar>
</template>
<template>
<TnTabbar v-model="currentTabbar" fixed>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
:bulge="index === 2"
bulge-bg-color="tn-gradient__cool-6"
/>
</TnTabbar>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
设置切换时动画
设置TnTabbar
的switch-animation
为true
可以开启切换时的动画效果,但是需要注意,凸起按钮暂时不支持切换动画
vue
<template>
<TnTabbar v-model="currentTabbar" fixed switch-animation>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
/>
</TnTabbar>
</template>
<template>
<TnTabbar v-model="currentTabbar" fixed switch-animation>
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
/>
</TnTabbar>
</template>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
切换拦截
通过TnTabbar
的before-switch
可以拦截切换事件,返回false
获取返回Promise
且被reject
可以阻止切换
vue
<script lang="ts" setup>
import { ref } from 'vue'
const currentTabbar = ref(0)
// 拦截第5个菜单
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const beforeSwitch = (index: number, name: string | number) => {
if (index === 4) {
uni.showToast({
icon: 'none',
title: '被拦截了',
})
return false
}
return true
}
</script>
<template>
<TnTabbar v-model="currentTabbar" fixed :before-switch="beforeSwitch">
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
/>
</TnTabbar>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const currentTabbar = ref(0)
// 拦截第5个菜单
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const beforeSwitch = (index: number, name: string | number) => {
if (index === 4) {
uni.showToast({
icon: 'none',
title: '被拦截了',
})
return false
}
return true
}
</script>
<template>
<TnTabbar v-model="currentTabbar" fixed :before-switch="beforeSwitch">
<TnTabbarItem
v-for="(item, index) in tabbarData"
:key="index"
:icon="item.icon"
:active-icon="item.activeIcon"
:text="item.name"
/>
</TnTabbar>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
API
Tabbar Props
属性名 | 说明 | 类型 | 默认值 | 可选值 |
---|---|---|---|---|
v-model/model-value | 当前选中的 Item 的索引值 | String | Number | - | - |
height | 导航栏高度,默认单位为 rpx,可以传入带单位的尺寸值 | String | - | - |
bulge-max-size | 凸起按钮的最大尺寸,单位 px | Number | 80 | - |
bulge-size-percent | 凸起按钮的尺寸比例,取值范围 0 ~ 1 | Number | 0.75 | - |
bg-color | 背景颜色,可以使用图鸟内置的背景色、hex、rgb、rgba,在frosted 为true 的时候只可以设置 rgba 类型的值 | String | - | - |
frosted | 开启毛玻璃效果,支付宝小程序不支持该参数 | Boolean | false | true |
inactive-color | 未选中时的颜色,支持图鸟内置的颜色值、hex、rgb、rgba | String | - | - |
active-color | 选中时的颜色,支持图鸟内置的颜色值、hex、rgb、rgba | String | - | - |
icon-size | 图标大小,默认单位为 rpx,可以传入带单位的尺寸值 | String | - | - |
font-size | 文字大小,默认单位为 rpx,可以传入带单位的尺寸值 | String | - | - |
top-shadow | 是否显示顶部阴影 | Boolean | true | false |
switch-animation | 切换 Item 动画 | Boolean | false | true |
fixed | 是否固定在底部 | Boolean | false | true |
safe-area-inset-bottom | 是否开启底部安全边距 | Boolean | true | false |
placeholder | 在固定之后是否开启占位空间 | Boolean | true | false |
before-switch | 切换前回调 | (index: number, name: string | number) => Promise<boolean> | boolean | - | - |
z-index | ZIndex | Number | 20090 | - |
hidden-fixed-tabbar | 隐藏固定在底部的导航栏 | Boolean | false | true |
Tabbar Emits
事件名 | 说明 | 类型 |
---|---|---|
change | item 切换时的回调函数 | `(name: string |
Tabbar Slots
插槽名 | 说明 | 子标签 |
---|---|---|
default | 导航栏内容 | TnTabbarItem |
Tabbar Expose
函数名 | 说明 | 类型 |
---|---|---|
setActiveItem | 手动设置当前激活的 item | (val: string | number) => void |
TabbarItem Props
属性名 | 说明 | 类型 | 默认值 | 可选值 |
---|---|---|---|---|
name | TnTabbarItem 的唯一值,如果没有传递则使用对应的索引值 | String | Number | - | - |
icon | 默认显示图标 | String | - | 图标有效值 |
active-icon | 选中时显示的图标 | String | - | 图标有效值 |
text | 显示的文本内容 | String | - | - |
inactive-color | 未选中时的颜色,支持图鸟内置的颜色值、hex、rgb、rgba | String | - | - |
active-color | 选中时的颜色,支持图鸟内置的颜色值、hex、rgb、rgba | String | - | - |
icon-size | 图标大小,默认单位为 rpx,可以传入带单位的尺寸值 | String | - | - |
font-size | 文字大小,默认单位为 rpx,可以传入带单位的尺寸值 | String | - | - |
bulge | 是否凸起显示 | Boolean | false | true |
bulge-bg-color | 凸起按钮的背景颜色,可以使用图鸟内置的背景色、hex、rgb、rgba,如果设置了该属性则inactive-color 和active-color 不会生效 | String | - | - |
bulge-text-color | 凸起按钮的图标颜色,支持图鸟内置的颜色值、hex、rgb、rgba | String | - | - |
badge | 设置角标的内容,如果为空或者不设置则不显示角标 | String | Number | - | - |
badge-config | 角标配置 | TabbarItemBadgeConfig | - | - |
disabled | 是否禁止点击 | Boolean | false | true |
TabbarItem Emits
事件名 | 说明 | 类型 |
---|---|---|
click | item 点击时的回调函数 | () => void |
TabbarItemBadgeConfig
参数 | 说明 | 必填 |
---|---|---|
dot | 显示点角标 | 否 |