微信小程序开发—— tabbar 配置

tabBar

如果小程序是一个多 tab 应用(客户端窗口的底部或顶部有 tab 栏可以切换页面),可以通过 tabBar 配置项指定 tab 栏的表现,以及 tab 切换时显示的对应页面。

如何在微信小程序中添加 tabBar 底部导航栏? 

直接在小程序根目录下的 app.json 中输入 "tabBar",微信开发者工具会自动帮你补全:{}

 tabBar 的属性

 

属性类型必填默认值描述
colorHexColortab 上的文字默认颜色,仅支持十六进制颜色
selectedColorHexColortab 上的文字选中时的颜色,仅支持十六进制颜色
backgroundColorHexColortab 的背景色,仅支持十六进制颜色
borderStylestringblacktabbar 上边框的颜色, 仅支持 black / white
listArraytab 的列表,详见 list 属性说明,最少 2 个、最多 5 个 tab
positionstringbottomtabBar 的位置,仅支持 bottom / top(当top时则没有icon)
custombooleanfalse自定义 tabBar

其中 list 接受一个数组,只能配置最少 2 个、最多 5 个 tab。tab 按数组的顺序排序,每个项都是一个对象,其属性值如下:

list 的属性

属性类型必填说明
pagePathstring页面路径,必须在 pages 中先定义
textstringtab 上按钮文字
iconPathstring图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px,不支持网络图片。
当 position 为 top 时,不显示 icon。
selectedIconPathstring选中时的图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px,不支持网络图片。
当 position 为 top 时,不显示 icon。

示例: app.json 中 tabBar 内的内容,如下

"tabBar": {
    "color": "#333",
    "selectedColor": "#ff0000",
    "backgroundColor": "#ffffff",
    "borderStyle": "black",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "/icons/_首页.png",
        "selectedIconPath": "/icons/首页.png"
      },
      {
        "pagePath": "pages/mine/mine",
        "text": "第二页面",
        "iconPath": "/icons/_我的.png",
        "selectedIconPath": "/icons/我的.png"
      }
    ]
  },

 

 

 

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Uniapp 小程序中,可以通过使用自定义导航栏来实现 TabBar 加 Icon 的效果。以下是一个简单的示例: 1. 在 `App.vue` 文件中,添加一个 `tabBar` 字段,用于定义底部 TabBar 的样式和内容: ```html <template> <div> <!-- 其他页面内容 --> <tab-bar :selected="selected" :list="tabBarList"></tab-bar> </div> </template> <script> import tabBar from '@/components/tabBar.vue' export default { components: { tabBar }, data() { return { tabBarList: [ { iconPath: '/static/tabbar/home.png', selectedIconPath: '/static/tabbar/home-active.png', text: '首页', pagePath: '/pages/index/index' }, { iconPath: '/static/tabbar/category.png', selectedIconPath: '/static/tabbar/category-active.png', text: '分类', pagePath: '/pages/category/category' }, // 添加更多的 TabBar 项 ], selected: 0 // 初始选中的 TabBar 项索引 } } } </script> ``` 2. 创建一个 `tabBar.vue` 组件,用于显示底部 TabBar 的内容: ```html <template> <div class="tab-bar"> <div v-for="(item, index) in list" :key="index" class="tab-bar-item" @click="changeTab(index)"> <img :src="selected === index ? item.selectedIconPath : item.iconPath" alt=""> <p :class="{ 'text-selected': selected === index }">{{ item.text }}</p> </div> </div> </template> <script> export default { props: { selected: { type: Number, default: 0 }, list: { type: Array, default: () => [] } }, methods: { changeTab(index) { if (index !== this.selected) { this.selected = index // 切换页面 uni.switchTab({ url: this.list[index].pagePath }) } } } } </script> <style scoped> .tab-bar { display: flex; justify-content: space-around; align-items: center; height: 50px; background-color: #f5f5f5; border-top: 1px solid #ddd; } .tab-bar-item { display: flex; flex-direction: column; align-items: center; } .tab-bar-item img { width: 24px; height: 24px; } .text-selected { color: #07c160; } </style> ``` 3. 在 `static` 目录下创建 `tabbar` 文件夹,并将对应的 TabBar 图标放入其中。 这样,就可以通过切换选项卡来改变页面,并在选中的选项卡上显示对应的图标和文本。你可以根据需要自定义 TabBar 的样式和图标
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值