微信小程序中【tabBar】的使用

1、tabBar:即底部导航栏,使用的时候,需要放在【app.json】文件目录下,同时,需要放在window的后面。

【app.json】文件用来对微信小程序进行全局配置,决定页面文件的路径、窗口表现、设置网络超时时间、设置多 tab 等。
在这里插入图片描述
2、tabBar的属性介绍

【color】 未选择时 底部导航文字的颜色

【selectColor 】 选择时 底部导航文字的颜色

【borderStyle】 底部导航边框的样色(默认为浅灰色线条)

【list】 导航配置数组

【pagePath】 页面访问地址

【text】 导航图标下方文字

【selectedIconPath】 选中时 图标路径

【iconPath】 未选择时 图标路径

{
  "pages":[
    "pages/index/index",
    "pages/favorite/favorite",
    "pages/cook/cook",
    "pages/my/my"
  ],
  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle":"black"
  },
  "style": "v2",
  "sitemapLocation": "sitemap.json",
  "tabBar": {
    "color": "#a9b7b7",
    "selectedColor": "#FF6347",
    "borderStyle":"black",
    "list": [
    {
    "pagePath": "pages/index/index",
    "text": "首页",
    "selectedIconPath": "pages/img/footer1-sel.png",
    "iconPath": "pages/img/footer1.png"
    },
    {
    "pagePath": "pages/favorite/favorite",
    "text": "心愿单",
    "selectedIconPath": "pages/img/footer2-sel.png",
    "iconPath": "pages/img/footer2.png"
    },
    {
      "pagePath": "pages/my/my",
      "text": "我的",
      "selectedIconPath": "pages/img/footer4-sel.png",
      "iconPath": "pages/img/footer4.png"
      }
    ]
    }
}

详细的样式设置可参看小程序的官方文档
https://developers.weixin.qq.com/miniprogram/dev/framework/config.html#全局配置

### 如何在微信小程序TabBar使用 IconFont #### 准备工作 为了能够在微信小程序使用 IconFont,在项目开始前需完成一些准备工作。这包括但不限于从 IconFont 官网挑选所需的图标并下载对应的字体文件,以及按照官方文档指导创建适用于微信小程序的 `.wxss` 文件[^1]。 #### 引入 IconFont 样式表 确保已将 `iconfont.wxss` 正确放置于合适路径下,并且已在全局样式文件 app.css 或者页面级 CSS 文件内通过如下方式引入该资源: ```css @import './path/to/your/iconfont.wxss'; ``` 此操作使得整个应用或特定页面能够识别来自 IconFont 的类名和相应样式[^3]。 #### 构建 TabBar 组件结构 构建包含多个子项(即各个标签页)的 TabBar 结构体。每个子项通常由两部分组成——代表图标的 `<view>` 和描述文字的 `<text>` 元素。对于动态切换选中状态的情况,则可以利用数据绑定机制来控制样式的变更。 ```html <view class="tabbar-box"> <block wx:for="{{list}}" wx:key="uniqueKey"> <view class="list-item {{currentIndex === index ? 'active' : ''}}" bindtap="handleTabClick" data-index="{{index}}"> <!-- 动态设置图标 --> <view class="iconfont {{item.iconPath}}"></view> <text>{{item.text}}</text> </view> </block> <!-- 可选:用于指示当前激活位置的滑动条 --> <view class="indicator-bar" style="transform: translateX({{currentIndex * (100 / list.length)}}%)"></view> </view> ``` 上述代码片段展示了如何遍历列表生成一系列带有不同图标的选项卡,并根据用户的点击事件更新活动状态下的索引值 `currentIndex` 来改变视觉效果[^4]。 #### 数据源配置 最后一步是准备作为模板渲染依据的数据集。这里假设存在一个名为 `list` 的数组变量存储着各标签的信息对象;每一个这样的对象至少应具备两个属性:一是指向所需显示的文字说明 (`name`) ,二是对应未被选中状态下所使用图标名称(`iconPath`) 。当某个标签处于高亮模式时,可以通过额外字段如 `selectedIconPath` 提供另一种形式的表现[^2]。 ```javascript Page({ data: { currentIndex: 0, list: [ { name: '首页', iconPath: 'icon-home', selectedIconPath: 'icon-home-active'}, { name: '分类', iconPath: 'icon-category', selectedIconPath: 'icon-category-active'}, { name: '购物车', iconPath: 'icon-cart', selectedIconPath: 'icon-cart-active'}, { name: '我的', iconPath: 'icon-profile', selectedIconPath: 'icon-profile-active'} ] }, handleTabClick(e){ const newIndex = e.currentTarget.dataset.index; this.setData({ currentIndex: newIndex }); } }) ``` 以上就是关于如何在微信小程序 TabBar 中集成 IconFont 图标的具体实现方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值