微信小程序自定义tabbar组件

 4362

这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了微信小程序自定义tabbar组件的具体代码,供大家参考,具体内容如下


由于项目需求,必须自己写组件:

第一步:在App.json中配置tabBar,自定也组件也必须配置,"custom": true,list里配置所有的tabbar页面。

  1. "tabBar": {
  2.     "custom": true,
  3.     "color": "red",
  4.     "selectedColor": "#3b81d7",
  5.     "backgroundColor": "#000000",
  6.     "list": [{
  7.         "pagePath": "pages/Role/InsureIndex/index",
  8.         "text": "首页"
  9.     }, {
  10.         "pagePath": "pages/Role/MineIndex/index",
  11.         "text": "首页"
  12.     }, {
  13.         "pagePath": "pages/index/userInfo/userInfo",
  14.         "text": "我的"
  15.     }]
  16. },

第二步:在pages的同级目录新建组件,文件夹名字:custom-tab-bar,自定义组件文件名为index。组件代码如下,应该都能看懂。

index.js

  1. Component({
  2.     properties: {},
  3.  
  4.     data: {
  5.         indexImg: "../static/images/tabBar/tab_icon_home_nor@2x.png",
  6.         indexSelectImg: "../static/images/tabBar/tab_icon_home_sel@2x.png",
  7.         aboutUsImg: "../static/images/tabBar/tab_icon_user_nor@2x.png",
  8.         aboutUsSelectImg: "../static/images/tabBar/tab_icon_user_sel@2x.png",
  9.         _tabbat: null,
  10.         iPhoneX: false,
  11.         urls: ['/pages/Role/InsureIndex/index',
  12.             '/pages/index/userInfo/userInfo'
  13.         ]
  14.     },
  15.     attached() {
  16.         var self = this
  17.         //此为业务代码,可不看
  18.         wx.getStorage({
  19.             key: 'userInfo',
  20.             success: function (res) {
  21.                 const {
  22.                     userRoleCode
  23.                 } = res.data
  24.                 if (userRoleCode == '50' || userRoleCode == '70') {
  25.                     self.setData({
  26.                         ["urls[0]"]: '/pages/Role/MineIndex/index'
  27.                     })
  28.                 } else if (userRoleCode == '30' || userRoleCode == '35' || userRoleCode == '40') {
  29.                     self.setData({
  30.                         ["urls[0]"]: '/pages/Role/InsureIndex/index'
  31.                     })
  32.                 }
  33.             }
  34.         })
  35.         wx.getSystemInfo({
  36.             success(res) {
  37.                 console.log(res.model)
  38.                 if (res.model.indexOf('iPhone X') >= 0) {
  39.                     self.setData({
  40.                         iPhoneX: true
  41.                     })
  42.                 }
  43.             }
  44.         })
  45.     },
  46.     /**
  47.      * 组件的方法列表
  48.      */
  49.     methods: {
  50.         switchTap: function (e) {
  51.             var self = this
  52.             var index = e.currentTarget.dataset.index;
  53.             var urls = self.data.urls
  54.             wx.switchTab({
  55.                 url: urls[index],
  56.             })
  57.         }
  58.     }
  59. })

index.wxml

  1. <div class="_tabbar {{iPhoneX?'_iPhoneX':''}}">
  2.     <div class="titem {{_tabbat==0?'tCdk':''}}" data-index="0" bind:tap="switchTap">
  3.         <image src="{{_tabbat==0?indexSelectImg:indexImg}}" />
  4.         <b>首页</b>
  5.     </div>
  6.     <div class="titem {{_tabbat==1?'tCdk':''}}" data-index="1" bind:tap="switchTap">
  7.         <image src="{{_tabbat==1?aboutUsSelectImg:aboutUsImg}}" />
  8.         <b>我的</b>
  9.     </div>
  10. </div>

index.wxss

  1. ._tabbar {
  2.     width: 100%;
  3.     height: 120rpx;
  4.     display: flex;
  5.     align-items: center;
  6.     background: #fff;
  7.     font-size: 26rpx;
  8.     color: #999999;
  9.     box-shadow: 0px -7rpx 13rpx 0px rgba(193, 185, 204, 0.13);
  10. }
  11.  
  12. ._tabbar .titem {
  13.     text-align: center;
  14.     width: 50%;
  15. }
  16.  
  17. ._tabbar .titem image {
  18.     display: block;
  19.     margin: auto;
  20.     width: 48rpx;
  21.     height: 48rpx;
  22.     margin-bottom: 10rpx;
  23. }
  24.  
  25. ._tabbar .tCdk {
  26.     color: #37ADFE;
  27. }
  28.  
  29. ._iPhoneX {
  30.     height: 148rpx;
  31. }

index.json

  1. {
  2.     "component": true,
  3.     "usingComponents": {}
  4. }

以上为组件代码,点击tabbar跳转页面时,会重新加载tabbar组件,导致选中样式一直是默认的,因此需要在用到tabbar的页面的js文件中做如下操作:(以 “首页” 页面为例)

  1. onShow: function () {
  2.     this.getTabBar().setData({
  3.         _tabbat: 0
  4.     })
  5. },

以上就已经完成了,但是看网上大家说会出现两个tabBar,我这边是没出现(一个自定义,一个自带的),如果出现的话,在app.js中的onLaunch函数中加入 wx.hideTabBar() , 隐藏自带的tabbar就可以了。


本文网址:https://www.zztuku.com/detail-8737.html
站长图库 - 微信小程序自定义tabbar组件
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐