微信小程序 自定义底部导航栏(tabBar)

微信小程序 是支持自定义导航栏的,可查看微信小程序官方文档,学习自定义tabBar的使用

1.创建自定义导航栏 目录和文件

注意:

  • 新建Component
  • 名字为 “ index”
  • 与pages同级

index.js文件

// custom-tab-bar/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    selected: 0,
    color: "#999999",
    selectedColor: "#EE3D42",
    listTab: [
      {
        pagePath: "/pages/index/index",
        text: "导航一",
        iconPath: "/static/guide_no.png",
        selectedIconPath: "/static/guide_active.png"
      },
      {
        pagePath: "/pages/calculate/calculate",
        text: "导航二",
        iconPath: "/static/calculate_no.png",
        selectedIconPath: "/static/calculate_active.png"
      },
      {
        pagePath: "/pages/personal/personal",
        text: "个人中心",
        iconPath: "/static/personal_no.png",
        selectedIconPath: "/static/personal_active.png"
      }
    ]
  },

  /**
   * 组件的方法列表
   */
  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

<!--custom-tab-bar/index.wxml-->
<cover-view class="tab-bar">
  <cover-view class="tab-bar-border"></cover-view>
  <cover-view wx:for="{{listTab}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <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>


index.wxss

/* custom-tab-bar/index.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;
  margin: 10rpx 0;
}

.tab-bar-item cover-view {
  font-size: 12px;
}

2.在app.json里添加,并配置 tabBar    custom:true

"tabBar": {
    "custom": true,
    "color": "#999999",
    "selectedColor": "#EE3D42",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "导航一",
        "iconPath": "static/guide_no.png",
        "selectedIconPath": "static/guide_active.png"
      },
      {
        "pagePath": "pages/calculate/calculate",
        "text": "导航二",
        "iconPath": "static/calculate_no.png",
        "selectedIconPath": "static/calculate_active.png"
      },
      {
        "pagePath": "pages/personal/personal",
        "text": "个人中心",
        "iconPath": "static/personal_no.png",
        "selectedIconPath": "static/personal_active.png"
      }
    ]
  },

 

注意:

点击导航栏时跳转不灵敏.可能会跳转两次

解决方案:

在添加的导航页面的 onShow函数里增加以下代码

if (typeof this.getTabBar === 'function' && this.getTabBar()) {
   this.getTabBar().setData({
     selected: 2 //0,1,2 0-导航一  1-导航二  2-个人中心
   })
}

底部导航栏隐藏或显示

1.自定义导航栏时

  tabBar配置: custom:true

custom-tab-bar下的index.wxml

<!--custom-tab-bar/index.wxml-->
<cover-view class="tab-bar" wx:if="{{showBar}}">
  <cover-view class="tab-bar-border"></cover-view>
  <cover-view wx:for="{{listTab}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <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>


custom-tab-bar下的index.js增加定义

showBar:true

在需要隐藏导航栏的页面调用 -- 隐藏 tabbar

this.getTabBar().setData({
  showBar: false
});

在需要隐藏导航栏的页面调用 -- 显示 tabbar

this.getTabBar().setData({
  showBar: true
});

2.配置导航栏时

隐藏tabbar 点击查看详细

wx.hideTabBar();

wx.hideTabBar(Object object)

基础库 1.9.0 开始支持,低版本需做兼容处理

隐藏 tabBar

参数

Object object

属性类型默认值必填说明
animationbooleanfalse是否需要动画效果
successfunction 接口调用成功的回调函数
failfunction 接口调用失败的回调函数
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

显示tabbar  点击查看详细

wx.showTabBar();

wx.showTabBar(Object object)

基础库 1.9.0 开始支持,低版本需做兼容处理

显示 tabBar

参数

Object object

属性类型默认值必填说明
animationbooleanfalse是否需要动画效果
successfunction 接口调用成功的回调函数
failfunction 接口调用失败的回调函数
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

 

  • 37
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 17
    评论
微信小程序tabBar是一种底部导航栏,用于在小程序中切换不同的页面。它由多个tab组成,每个tab对应一个页面。tabBar可以通过配置文件进行设置,包括背景色、选中时的图片路径、边框颜色、文字颜色等属性。\[1\]\[2\] 在配置文件中,可以设置tabBar的位置、边框颜色、文字颜色、选中时的文字颜色、背景色等属性。同时,还需要配置tab页签的列表,最少需要2个tab,最多可以有5个tab。每个tab需要设置选中时和未选中时的图片路径、对应的页面路径和显示的文字。\[3\] 通过配置tabBar,用户可以方便地在小程序中切换不同的页面,提供了良好的用户体验。 #### 引用[.reference_title] - *1* [微信小程序Tabbar](https://blog.csdn.net/qq_25846091/article/details/113107745)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【微信小程序】全局配置 - tabBar](https://blog.csdn.net/Javascript_tsj/article/details/125711905)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [微信小程序TabBar的使用](https://blog.csdn.net/weixin_43294092/article/details/107305403)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黎轩栀海

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

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

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

打赏作者

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

抵扣说明:

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

余额充值