程序员scholar 程序员scholar
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
npm

(进入注册为作者充电)

  • 原生微信小程序

  • uniapp多端开发

    • 快速入门

    • 内置组件

      • 视图容器

      • 基础内容

      • 表单组件

      • 路由页面跳转

      • 媒体组件

      • 地图

      • 画布

      • 网页嵌入

      • 页面属性配置节点

        • 页面元信息组件
        • 导航栏组件
        • 自定义底部导航栏组件
          • 1. 什么是 custom-tab-bar 组件?
          • 2. custom-tab-bar 组件的常用属性
            • 2.1 设置 direction 属性
            • 2.2 显示 tabBar 图标
            • 2.3 处理 tabBar 点击事件
          • 3. 自定义水平与垂直 tabBar
      • nvue组件

      • 小程序组件

    • 扩展组件

  • 小程序开发
  • uniapp多端开发
  • 内置组件
  • 页面属性配置节点
scholar
2024-10-21
目录

自定义底部导航栏组件

# 自定义底部导航栏组件

custom-tab-bar 是 Uniapp 中用于自定义页面底部或顶部导航栏的组件,主要用于 H5 平台。与原生 tabBar 相比,custom-tab-bar 提供了更灵活的布局和样式配置,特别适合 PC 端的宽屏布局。

# 1. 什么是 custom-tab-bar 组件?

custom-tab-bar 组件允许开发者根据需求自定义页面的导航栏(tabBar),并摆放在顶部、底部或页面的任意位置。与 pages.json 中的原生 tabBar 不同,自定义 tabBar 更灵活,特别适合 H5 端的自定义布局需求。它仍然读取 pages.json 中的 tabBar 配置信息,同时支持与 tabBar 相关的所有 API,例如设置 tab 徽标、显示红点以及 switchTab 等。

使用场景

  • 顶部导航:在 PC 端使用自定义的 tabBar 放置在页面顶部,作为一级导航栏。
  • 自定义布局:根据页面需求自由配置 tabBar 的排列方向、图标、样式等。
  • 跨页面共享导航:通过自定义组件实现跨页面共享导航结构。

# 2. custom-tab-bar 组件的常用属性

custom-tab-bar 提供了丰富的属性用于配置 tabBar 的样式及行为。以下是常用属性的详细说明及使用示例:

属性名 类型 默认值 说明
direction String horizontal 选项的排列方向,可选值:horizontal(水平),vertical(垂直)
show-icon Boolean false 是否显示 tabBar 项的图标
selected Number 0 选中的 tabBar 项索引值,从 0 开始
onTabItemTap EventHandle tabBar 项目被点击时的事件回调,返回被点击的 tab 项相关信息

# 2.1 设置 direction 属性

  • 说明:通过 direction 属性设置 tabBar 选项的排列方向,可以设置为水平或垂直排列。
  • 类型:String
  • 默认值:horizontal
<template>
  <view>
    <!-- 水平排列的 tabBar -->
    <custom-tab-bar direction="horizontal" :selected="selected" @onTabItemTap="onTabItemTap" />
    
    <!-- 垂直排列的 tabBar -->
    <custom-tab-bar direction="vertical" :selected="selected" @onTabItemTap="onTabItemTap" />
  </view>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const selected = ref(0);  // 当前选中的 tabBar 项索引

    const onTabItemTap = (item) => {
      console.log('点击的 tab 项:', item);
    };

    return { selected, onTabItemTap };
  }
}
</script>
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

# 2.2 显示 tabBar 图标

  • 说明:通过 show-icon 属性控制是否显示 tabBar 项的图标。
  • 类型:Boolean
  • 默认值:false
<template>
  <view>
    <!-- 显示图标的 tabBar -->
    <custom-tab-bar direction="horizontal" :show-icon="true" :selected="selected" @onTabItemTap="onTabItemTap" />
    
    <!-- 不显示图标的 tabBar -->
    <custom-tab-bar direction="horizontal" :show-icon="false" :selected="selected" @onTabItemTap="onTabItemTap" />
  </view>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const selected = ref(1);  // 当前选中的 tabBar 项索引

    const onTabItemTap = (item) => {
      console.log('点击的 tab 项:', item);
    };

    return { selected, onTabItemTap };
  }
}
</script>
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

# 2.3 处理 tabBar 点击事件

  • 说明:通过 onTabItemTap 事件监听 tabBar 项的点击行为,并处理点击事件。
  • 类型:EventHandle
  • 默认值:无
<template>
  <view>
    <!-- 处理 tabBar 点击事件 -->
    <custom-tab-bar direction="horizontal" :selected="selected" @onTabItemTap="onTabItemTap" />
  </view>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const selected = ref(0);  // 当前选中的 tabBar 项索引

    const onTabItemTap = (item) => {
      console.log('被点击的 tab 项:', item.index, item.text);
      selected.value = item.index;  // 更新当前选中的 tabBar 项
    };

    return { selected, onTabItemTap };
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 3. 自定义水平与垂直 tabBar

以下案例展示了如何使用 custom-tab-bar 实现水平与垂直排列的 tabBar,包含图标、点击事件处理等完整功能。

<template>
  <view>
    <!-- 水平排列的 tabBar,显示图标 -->
    <custom-tab-bar
      direction="horizontal"
      :show-icon="true"
      :selected="selected"
      @onTabItemTap="onTabItemTap"
    />

    <!-- 垂直排列的 tabBar,不显示图标 -->
    <custom-tab-bar
      direction="vertical"
      :show-icon="false"
      :selected="selected"
      @onTabItemTap="onTabItemTap"
    />
  </view>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const selected = ref(0);  // 当前选中的 tabBar 项索引

    // 处理 tabBar 点击事件
    const onTabItemTap = (item) => {
      console.log('点击的 tab 项:', item.index, item.text);
      selected.value = item.index;  // 更新选中的 tabBar 项
    };

    return { selected, onTabItemTap };
  }
}
</script>
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
编辑此页 (opens new window)
上次更新: 2025/02/01, 02:18:15
导航栏组件
条形码/二维码组件

← 导航栏组件 条形码/二维码组件→

Theme by Vdoing | Copyright © 2019-2025 程序员scholar
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式