首先我们先看一下具体功能
一、修改 app.json 文件,将 tabBar修改为自定义tab,list 保持不变,最多5个
"pagePath": "pages/main-HY/index", "iconPath": "/images/tabbar/货源icon48*48@2x.png", "selectedIconPath": "/images/tabbar/货源icon@2x.png" "pagePath": "pages/main-SXY/index", "iconPath": "/images/tabbar/商学院icon@2x.png", "selectedIconPath": "/images/tabbar/商学院icon48*48@2x.png" "pagePath": "pages/main-DZZX/index", "iconPath": "/images/tabbar/店主中心icon@2x.png", "selectedIconPath": "/images/tabbar/店主中心icon48*48@2x.png"
二、在项目的根目录新建 custom-tab-bar,这个文件就是微信已经帮我们定义好了,只需要新增文件,就可以微信自动读取
三、custom-tab-bar/index.wxml 写入,官网中提供,使用 cover-view 标签来操作,目前我这边是使用view,因为 cover-view 在向上滑动的时候,会带着tab 一起拖上去,不太好看,
<!--custom-tab-bar/index.wxml--> <view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab"> <view class="center_part" wx:if="{{item.iconPath}}"> <image class="center_img center-has-noimg" src=""></image> <image class="center_img center-has-image" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"> <view class="txt fontWeight bg_rec" wx:if="{{selected === index}}"> <view class="txt " wx:if="{{selected != index}}">{{item.text}}</view>
四、custom-tab-bar/index.js 写入
// custom-tab-bar/index.js selectedColor: "#FF1C6C", "pagePath": "/pages/main-HY/index", "iconPath": "/images/tabbar/货源icon48*48@2x.png", "selectedIconPath": "/images/tabbar/货源icon@2x.png" "pagePath": "/pages/main-SXY/index", "iconPath": "/images/tabbar/商学院icon@2x.png", "selectedIconPath": "/images/tabbar/商学院icon48*48@2x.png" "pagePath": "/pages/main-DZZX/index", "iconPath": "/images/tabbar/店主中心icon@2x.png", "selectedIconPath": "/images/tabbar/店主中心icon48*48@2x.png" // const idx = e.currentTarget.dataset.index // const path = e.currentTarget.dataset.path // if (this.data.selected==0) { // url: '/pages/main-HY/index', // }else if(this.data.selected==1){ // url: '/pages/main-SXY/index', // url: '/pages/main-DZZX/index', // console.log(this.data.selected, idx) const idx = e.currentTarget.dataset.index const path = e.currentTarget.dataset.path
五、custom-tab-bar/index.wxss 写入
/* custom-tab-bar/index.wxss */ padding-bottom: env(safe-area-inset-bottom); border-radius: 30rpx 30rpx 0 0; border-top: 1px solid #eaeaea; .tab-bar-item cover-image { .tab-bar-item cover-view { transform: translate(-50%); justify-content: space-evenly;
六、总结踩过的坑
1. 进入首页,点击其他tab,页面会刷新,tabindex 会再次回到首页
解决方案:在每个 switchtab页面 中写入以下 ,( switchtab页面为tabBar 中list 配置的页面)
onShow:function (params) { if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 0 // 数字是当前页面在tabbar的索引,如我的查询页索引是2,因此这边为2,同理首页就为0,消息中心页面为1
|