微信小程序自定义tabbar栏实现过程讲解

 更新时间:2023年02月01日 09:23:15   作者:山山而川~xyj  
tabBar相对而言用的还是比较多的,但是用起来并没有难,下面这篇文章主要给大家介绍了关于微信小程序全局配置之tabBar的相关资料,文中通过图文以及示例代码介绍的非常详细,需要的朋友可以参考下

前言

昨天主管突然给我说微信小程序默认的 tabBar 不美观,让我改成中间突出的那种样式。纵然我心里面有千般不情愿,但还是接下了这个任务。查了一下文档 自定义 tabBar 发现有这个方法,有思路了就赶紧搞起来,以下是我的开发经验分享。

一、自定义tabbar栏配置

  • 在 app.json 文件中的 tabBar 中指定 custom 字段为 true(意思是允许使用自定义 tabBar);
  • 在 app.json 中全局开启使用组件,或者在所有涉及的 tab 页 json 中申明usingComponents 项;
  • 在 app.json 中添加作为 tabBar 栏的页面;

示例代码

  "tabBar": {
    "custom": true,
    "color": "#afafaf",
    "selectedColor": "#0099f6",
    "backgroundColor": "#F7F8F8",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页"
      },
      {
        "pagePath": "pages/goodIndexCopy/goodIndexCopy",
        "text": "易购商城"
      },
      {
        "pagePath": "pages/release/release",
        "text": "发布"
      },
      {
        "pagePath": "pages/nearby/nearby",
        "text": "本地"
      },
      {
        "pagePath": "pages/mine/mine",
        "text": "我的"
      }
    ]
  },
  "usingComponents": {},

pagePath 是自己添加的页面,text 是tabBar上展示的文字。

二、添加自定义tabbar栏组件

在根目录下创建 custom-tab-bar 文件夹,并在该文件夹下新建 Component,或者新建 Page,但是这种创建方式需要自己改动一些代码,在这里我们选用新建 Component 的方式。

添加组件代码

1、完善 wxml 文件代码,tabBar 栏需要展示的页面是一个固定的数组,可以使用 wx:for 循环展示,在这里用到 selected 这个字段,这个字段的作用是帮助展示 tabBar 选中和未选中的样式。

<!--custom-tab-bar/index.wxml-->
<view class="tab-bar">
  <view wx:for="{{list}}" wx:key="index" class="tab-bar-item {{item.bulge?'bulge':''}}" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <view  wx:if="item.bulge" class="tab-bar-bulge"></view>
    <image class="image" src="{{selected == index ? item.selectedIconPath : item.iconPath}}"></image>
    <!-- <view  wx:if="{{item.text}}" style="color: {{selected === index ? selectedColor : color}}" class="tab-bar-view">{{item.text}}</view> -->
    <view  class="tab-bar-view" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
  </view>
</view>

2、完善 js 文件代码,list 数组就是在 tabBar 栏展示的页面信息,switchTab 方法作用可以出看来是负责跳转页面。其它的字段相信各位都知道,这里就不再描述。

  /**
   * 组件的初始数据
   */
  data: {
    selected: 0,
    color: "#afafaf",
    selectedColor: "#0099f6",
    backgroundColor: "#F7F8F8",
    list: [
      {
        pagePath: "/pages/index/index",
        iconPath: "/images/icon/wtc/icon_zhuye2.png",
        selectedIconPath: "/images/icon/wtc/icon_zhuye2_d.png",
        text: "首页",
      },
      {
        pagePath: "/pages/goodIndexCopy/goodIndexCopy",
        iconPath: "/images/icon/wtc/icon_pintuan2.png",
        selectedIconPath: "/images/icon/wtc/icon_pintuan2_d.png",
        text: "易购商城"
      },
      {
        pagePath: "/pages/release/release",
        bulge:true,
        iconPath: "/images/add.png",
        selectedIconPath: "/images/add-active.png",
        text: "发布"
      },
      {
        pagePath: "/pages/nearby/nearby",
        iconPath: "/images/icon/wtc/icon_pintuan3.png",
        selectedIconPath: "/images/icon/wtc/icon_pintuan3_d.png",
        text: "本地",
      },
      {
        pagePath: "/pages/mine/mine",
        iconPath: "/images/icon/wtc/icon_wode3.png",
        selectedIconPath: "/images/icon/wtc/icon_wode3_d.png",
        text: "我的"
      },
    ]
  },
    /**
   * 组件的方法列表
   */
  methods: {
    switchTab(e) {
      // console.log(e);
      const data = e.currentTarget.dataset;
      const url = data.path;
      wx.switchTab({url})
    }
  }

3、完善 wxss 文件代码。

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  background: #FFF;
  display: flex;
  line-height: 1.2;
  padding-bottom: env(safe-area-inset-bottom);
  border-top: 1px solid #e6e6e6;
}
.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.tab-bar-item .image {
  width: 26px;
  height: 26px;
}
.bulge {
  background-color: #FFF;
}
.bulge .tab-bar-bulge{
  position: absolute;
  z-index: -1;
  width: 64px;
  height: 64px;
  top: -24px;
  border-radius: 50%;
  border-top: 1px solid #e6e6e6;
  background-color: #FFF;
}
.bulge .image{
  position: absolute; 
  width: 50px;
  height: 50px;
  top: -20px;
}
.bulge .tab-bar-view{
  position: relative;
  bottom: -16px;
  margin-top: 4px;
}
.tab-bar-item .tab-bar-view {
  font-size: 12px;
  margin-top: 4px;
}

创建全局字段

做完以上工作之后,我们可以就可以看一下效果了,是不是就以为这样就可以了呢?但是事与愿违,会发现这里存在 bug,在我们点击 tabBar 栏进行跳转的时候会发现页面跳转过去了,但是对应页面的 tabBar 没有改变颜色。为了解决这个 bug,需要添加全局字段。在 app.js 文件中创建该字段。

  globalData: {
    selected: 0
  },

在组件中保存重要字段

全局字段创建完成之后,我们需要在组件 js 文件中使用该字段,在 ready 函数中保存这个字段,在点击 tabBar 栏时,把相应的index 赋值给这个全局字段。

// 引入全局函数
const app = getApp()
Component({
  /**
   * 组件的初始数据
   */
  data: {
    selected: 0,
    color: "#afafaf",
    selectedColor: "#0099f6",
    backgroundColor: "#F7F8F8",
    list: [
      {
        pagePath: "/pages/index/index",
        iconPath: "/images/icon/wtc/icon_zhuye2.png",
        selectedIconPath: "/images/icon/wtc/icon_zhuye2_d.png",
        text: "首页",
      },
      {
        pagePath: "/pages/goodIndexCopy/goodIndexCopy",
        iconPath: "/images/icon/wtc/icon_pintuan2.png",
        selectedIconPath: "/images/icon/wtc/icon_pintuan2_d.png",
        text: "易购商城"
      },
      {
        pagePath: "/pages/release/release",
        bulge:true,
        iconPath: "/images/add.png",
        selectedIconPath: "/images/add-active.png",
        text: "发布"
      },
      {
        pagePath: "/pages/nearby/nearby",
        iconPath: "/images/icon/wtc/icon_pintuan3.png",
        selectedIconPath: "/images/icon/wtc/icon_pintuan3_d.png",
        text: "本地",
      },
      {
        pagePath: "/pages/mine/mine",
        iconPath: "/images/icon/wtc/icon_wode3.png",
        selectedIconPath: "/images/icon/wtc/icon_wode3_d.png",
        text: "我的"
      },
    ]
  },
  ready: function() {
    this.setData({
      selected: app.globalData.selected
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    switchTab(e) {
      // console.log(e);
      const data = e.currentTarget.dataset;
      const url = data.path;
      app.globalData.selected = data.index;
      wx.switchTab({url})
    }
  }
})

添加完成后,可以测试一下效果,发现刚才的 bug 已经解决。perfect!!

三、效果展示

到此这篇关于微信小程序自定义tabbar栏实现过程讲解的文章就介绍到这了,更多相关小程序tabbar栏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JavaScript预解析及相关技巧分析

    JavaScript预解析及相关技巧分析

    这篇文章主要介绍了JavaScript预解析及相关技巧,结合实例形式分析了JavaScript与解析的原理,步骤与相关技巧,需要的朋友可以参考下
    2016-04-04
  • Typescript中的数据类型实例总结

    Typescript中的数据类型实例总结

    ts中数据类型的定义是重点之一,所以必须明确ts有哪些数据类型,下面这篇文章主要给大家介绍了关于Typescript中数据类型的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-03-03
  • ES6新特性八:async函数用法实例详解

    ES6新特性八:async函数用法实例详解

    这篇文章主要介绍了ES6新特性八:async函数用法,结合实例形式分析了async函数的功能、原理、使用方法与相关注意事项,需要的朋友可以参考下
    2017-04-04
  • js弹出框、对话框、提示框、弹窗实现方法总结(推荐)

    js弹出框、对话框、提示框、弹窗实现方法总结(推荐)

    下面小编就为大家带来一篇js弹出框、对话框、提示框、弹窗实现方法总结(推荐)。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • 关于JS中prototype的理解

    关于JS中prototype的理解

    在js中phototype是JS中比较难理解的一个部分,下面通过本篇文章给大家介绍js中的prototype,需要的朋友可以参考下
    2015-09-09
  • JavaScript对象属性遍历与描述详解

    JavaScript对象属性遍历与描述详解

    在 JavaScript 中,对象是一种非常重要的数据类型,理解如何遍历对象的属性以及对象属性的描述,对于高效使用 JavaScript 至关重要,本文将详细介绍对象属性的遍历方法和属性描述对象的相关内容,需要的朋友可以参考下
    2025-02-02
  • 建议大家看下JavaScript重要知识更新

    建议大家看下JavaScript重要知识更新

    建议大家看下JavaScript重要知识更新...
    2007-07-07
  • JavaScript中小数点精度丢失的原因以及解决方法

    JavaScript中小数点精度丢失的原因以及解决方法

    计算机再大的内存它也存不下,所以不能存储一个相对于数学来说的值,只能存储一个近似值,所以当计算机存储后再取出来用时就会出现精度问题,下面这篇文章主要给大家介绍了关于JavaScript中小数点精度丢失的原因以及解决方法,需要的朋友可以参考下
    2023-10-10
  • js实现简单抽奖小功能

    js实现简单抽奖小功能

    这篇文章主要为大家详细介绍了js实现简单抽奖小功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • 微信小程序云开发实现云数据库读写权限

    微信小程序云开发实现云数据库读写权限

    这篇文章主要为大家详细介绍了微信小程序云开发实现云数据库读写权限,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05

最新评论