小程序tabBar自定义全套

小程序自定义tabBar 注意事项!!!

  1. 组件位置需要与pages在同一级目录
  2. 文件夹名称必须是 custom-tab-bar
  3. 文件名必须是 index.后缀
  4. 在这里插入图片描述

业务需求需要自定义tabbar

自定位 custom-tab-bar 文件 如下:

例如需要判断登录问题

  1. index.js 页面
// components/Tabbar/tabbar.js
import storage from '../utils/cache'

Component({
  /**
 1. 组件的属性列表
   */
  properties: {

  },

  /**
 2. 组件的初始数据
   */
  data: {
    Zindex:9999,
    color: "#272822",
    selectedColor: "#464646",
    selected:0,
    list: [{
        pagePath: "/pages/home/home",
        text: "首页",
        iconPath: "../images/home.png",
        selectedIconPath: "../images/home_s.png"
      },
      {
        pagePath: "/pages/timer/timer",
        text: "计时",
        iconPath: "../images/time.png",
        selectedIconPath: "../images/time_s.png"

      }, {
        pagePath: "/pages/list/list",
        text: "排行",
        iconPath: "../images/list.png",
        selectedIconPath: "../images/list_s.png"
      }, {
        pagePath: "/pages/about/about",
        text: "我的",
        iconPath: "../images/about.png",
        selectedIconPath: "../images/about_s.png"
      }
    ]

  },

  /**
 3. 组件的方法列表
   */
  methods: {
    onChange(e){
      // let url=e.detail
      const data = e.currentTarget.dataset
      console.log(e)
      const url = data.path
      if (!storage.getToken()) {
          wx.navigateTo({
            url: '/pages/login/login'	//能够带参数,在登陆页面接收
          })
          return;
      }
      this.setData({
          selected: data.index
      })
      wx.switchTab({
          url
      })
    }
  }
})
  1. index.html 页面
这里官网也说了 最好用 cover-view  cover-image 主要是层级问题  哈哈
<cover-view class="tab-bar">
  <cover-view class="tab-bar-border"></cover-view>
  <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="onChange">
    <cover-image src="{{selected == index ? item.selectedIconPath : item.iconPath}}"></cover-image>
    <cover-view style="color: {{selected == index ? selectedColor : color}}">{{item.text}}</cover-view>
  </cover-view>
</cover-view>
  1. index.wsxx
/* components/Tabbar/tabbar.wxss */
.tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 48px;
    background: white;
    display: flex;
    padding-bottom: env(safe-area-inset-bottom);
  }
  
  .tab-bar-border {
    background-color: rgba(0, 0, 0, 0.33);
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 1px;
    transform: scaleY(0.5);
  }
  
  .tab-bar-item {
    flex: 1;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }
  
  .tab-bar-item cover-image {
    width: 27px;
    height: 27px;
  }
  
  .tab-bar-item cover-view {
    font-size: 10px;
  }

别忘记主要的 app.json

在这里插入图片描述

最后页面需要 switchpage 页面加上

  tabbershow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0 // 注意这个防止点击tab 样式没跟上!!!! 
      })
    }
  },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值