微信小程序自定义tabBar以及图标-使用vant-weapp

小程序整合vant weapp可以看《微信小程序vant weapp安装与使用

微信官方文档有介绍自定义tabBar

1、在小程序根目录下创建custom-tab-bar文件夹,并创建以下文件。(这个是作为入口文件的

custom-tab-bar/index.js
custom-tab-bar/index.json
custom-tab-bar/index.wxml
custom-tab-bar/index.wxss

2、修改custom-tab-bar/index.js (清除初始化的内容)

Component({
  data: { 
    selected: 0,
    list: [
      {
        pagePath: "/pages/checkin/checkin",
        text: "打卡",
        normal: '/images/tabbar/position.png',
        active: '/images/tabbar/position-green.png'
      },
      {
        pagePath: "/pages/idcard/idcard",
        text: "上传",
        normal: '/images/tabbar/idcard.png',
        active: '/images/tabbar/idcard-green.png'
      }
    ]
  },
  methods: {
    onChange(e) {
       console.log(e,'e')
       this.setData({ active: e.detail });
       wx.switchTab({
         url: this.data.list[e.detail].pagePath
       });
    },
    init() {
        const page = getCurrentPages().pop();
        this.setData({
          active: this.data.list.findIndex(item => item.pagePath === `/${page.route}`)
        });
       }
    }
})

3、修改custom-tab-bar/index.json

{
  "component": true,
    "usingComponents": {
      "van-tabbar": "@vant/weapp/tabbar/index",
      "van-tabbar-item": "@vant/weapp/tabbar-item/index",
      "van-icon": "@vant/weapp/icon/index"
    }
}

4、 修改custom-tab-bar/index.wxml

       代码中两个imgage是自定义图标的,未选中是item.normal图片,选中了是item.active

<van-tabbar active="{{ active }}" bind:change="onChange" active-color="#07c160" inactive-color="gainsboro">
  <van-tabbar-item wx:for="{{ list }}" wx:key="index" >
    <image
      slot="icon"
      src="{{ item.normal }}"
      mode="aspectFit"
      style="width: 30px; height: 18px;"
    />
    <image
      slot="icon-active"
      src="{{ item.active }}"
      mode="aspectFit"
      style="width: 30px; height: 18px;"
    /> 
    {{item.text}}
  </van-tabbar-item>
</van-tabbar>

5、在app.json添加tabBar

"tabBar": {
    "custom": true,
    "color": "#000000",
    "selectedColor": "#000000",
    "backgroundColor": "#000000",
    "list": [
      {
        "pagePath": "pages/checkin/checkin",
        "text": "打卡"
      },
      {
        "pagePath": "pages/idcard/idcard",
        "text": "上传"
      }
    ]
  }

 6、每个tabBar页面的onShow()方法添加

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTabBar().init();
  },

效果图如下

 

源码下载:自定义tabBar

 

 

  • 13
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值