微信小程序自定义tabbar

(1): 在app.json配置如下,主要是custon配置和usingComponents
"tabBar": {
    "custom": true,
    "color": "#999999",
    "selectedColor": "#000000",
    "borderStyle": "black",
    "backgroundColor": "#FFFFFF",
    "list": [
      {
        "pagePath": "pages/index/index",
        "iconPath": "static/tabbar/tab-home.png",
        "selectedIconPath": "static/tabbar/tab-home-sel.png",
        "text": "首页"
      },
      {
        "pagePath": "pages/hot/hot",
        "iconPath": "static/tabbar/tab-hot.png",
        "selectedIconPath": "static/tabbar/tab-hot-sel.png",
        "text": "今日爆款"
      },
      {
        "pagePath": "pages/cart/cart",
        "iconPath": "static/tabbar/tab-cart.png",
        "selectedIconPath": "static/tabbar/tab-cart-sel.png",
        "text": "购物车"
      },
      {
        "pagePath": "pages/mine/mine",
        "iconPath": "static/tabbar/tab-mine.png",
        "selectedIconPath": "static/tabbar/tab-mine-sel.png",
        "text": "我的"
      }
    ]
  },
  "usingComponents": {}
(2): 新建custom-tab-bar组件(根目录下)
  • index.js
Component({
  data: {
    'selected': 0,
    'color': "#ccc",
    "selectedColor": "#000000",
    "list": [
      {
        "pagePath": "/pages/index/index",
        "iconPath": "/static/tabbar/tab-home.png",
        "selectedIconPath": "/static/tabbar/tab-home-sel.png",
        "text": "首页"
      },  
      {
        "pagePath": "/pages/hot/hot",
        "iconPath": "/static/tabbar/tab-hot.png",
        "selectedIconPath": "/static/tabbar/tab-hot-sel.png",
        "text": "今日爆款"
      },
      {
        "pagePath": "/pages/cart/cart",
        "iconPath": "/static/tabbar/tab-cart.png",
        "selectedIconPath": "/static/tabbar/tab-cart-sel.png",
        "text": "购物车"
      },
      {
        "pagePath": "/pages/mine/mine",
        "iconPath": "/static/tabbar/tab-mine.png",
        "selectedIconPath": "/static/tabbar/tab-mine-sel.png",
        "text": "我的"
      }
    ]
  },
  attached() {
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({url})
      // this.setData({
      //   selected: data.index
      // })
    }
  }
})

  • index.json
{
  "component": true
}
  • index.wxml
<cover-view class="tab-bar">
  <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <cover-image class='tab-bar-img' src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
    <cover-view class="tab-bar-text" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
  </cover-view>
</cover-view>

  • index.wxss
/* custom-tab-bar/index.wxss */

.tab-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  display: flex;
  justify-content: space-around;
  background-color: #eef0f4;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.tab-bar-img {
  width: 24px;
  height: 24px;
  margin-bottom: 2px;
}
.tab-bar-text{
  font-size: 10px;
}
.selectedColor {
  color: #f60;
}

(3) 在每个tabbar中的onShow中配置
  onShow() {
    // 页面显示
    if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0, // 对应当前页面在tabbar--list中的索引 0 1 2 3(代表四个tabbar)
          show: true
        })
      }
  },
显示如下,但是有个问题就是页面第一次切换,会闪动一下,也不知道怎么解决,希望大佬给个提示

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxslxskxs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值