小程序全局配置 - tabBar

1. 什么是 tabBar

tabBar 是移动端应用常见的页面效果,用于实现多页面的快速切换。小程序中通常将其分为:

  • 底部 tabBar
  • 顶部 tabBar

注意:

  • tabBar中只能配置最少 2 个、最多 5 个 tab 页签
  • 当渲染顶部 tabBar 时,不显示 icon,只显示文本

2 tabBar 的 6 个组成部分

  1. backgroundColor:tabBar 的背景色
  2. borderStyle:tabBar 上边框的颜色
  3. color:tab 上文字的默认(未选中)颜色
  4. iconPath:未选中时的图片路径
  5. selectedIconPath:选中时的图片路径
  6. selectedColor:tab 上的文字选中时的颜色

3 tabBar 节点的配置项

属性类型必填默认值描述
listArraytab 页签的列表,最少 2 个、最多 5 个 tab
positionStringbottomtabBar 的位置,仅支持 bottom/top
backgroundColorHexColortabBar 的背景色
borderStyleStringblacktabBar 上边框的颜色,仅支持 black/white
colorHexColortab 上文字的默认(未选中)颜色
selectedColorHexColortab上的文字选中时的颜色

4 每个 tab 项的配置选项

属性类型必填描述
pagePathString页面路径,页面必须在 pages 中预先定义
textStringtab 上显示的文字
iconPathString未选中时的图标路径;当 postion 为 top 时,不显示 icon
selectedIconPathString选中时的图标路径;当 postion 为 top 时,不显示 icon

5 案例:配置 tabBar

5.1 新建 3 个对应的 tab 页面

通过 app.json 文件的 pages 节点,快速新建 3 个对应的 tab 页面
app.json

  "pages": [
    "pages/home/home",
    "pages/message/message",
    "pages/contact/contact",
    
    "pages/list/list",
    "pages/index/index",
    "pages/logs/logs"
    
  ],

5.2 配置 tabBar 选项

  1. ① 打开 app.json 配置文件,和 pages、window 平级,新增 tabBar 节点
  2. ② tabBar 节点中,新增 list 数组,这个数组中存放的,是每个 tab 项的配置对象
  3. ③ 在 list 数组中,新增每一个 tab 项的配置对象。对象中包含的属性如下:
    • pagePath 指定当前 tab 对应的页面路径【必填】
    • text 指定当前 tab 上按钮的文字【必填】
    • iconPath 指定当前 tab 未选中时候的图片路径【可选】
    • selectedIconPath 指定当前 tab 被选中后高亮的图片路径【可选】
 "tabBar": {
    "position": "bottom",
    "list": [
      {
        "pagePath": "pages/home/home",
        "text": "首页",
        "iconPath": "/images/tabs/home.png",
        "selectedIconPath": "/images/tabs/home-active.png"
      },
      {
        "pagePath": "pages/message/message",
        "text": "消息",
        "iconPath": "/images/tabs/message.png",
        "selectedIconPath": "/images/tabs/message-active.png"
      },
      {
        "pagePath": "pages/contact/contact",
        "text": "联系我们",
        "iconPath": "/images/tabs/contact.png",
        "selectedIconPath": "/images/tabs/contact-active.png"
      }

    ]
  },

运行效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值