首先附上微信小程序自定义tabbar开发文档地址:传送门
一直使用的都是微信原生的tabbar组件,这一次想给小程序加一个功能,实现首页弹窗轮播图全屏覆盖(弹窗遮罩层盖住小程序原生的头部和底部导航组件),小程序原生的底部tabbar组件默认层级是最高的,无法被其余组件和标签遮盖,要实现遮罩层盖住tabbar组件只能舍弃掉原生的tabbar,自定义一个tabbar了。
先看我的小程序的层级结构:
接下来是自定义tabbar组件的开发步骤:
//wxml <van-tabbar active="{{ active }}" bind:change="onChange"> <van-tabbar-item wx:for="{{ list }}" wx:key="index" icon="{{active === index ? item.selectedIconPath : item.iconPath}}"> <text style="color: {{active === index ? selectedColor : color}}">{{item.text}}</text> </van-tabbar-item> </van-tabbar>
//js Component({ data: { active: '', color: "#C2C6D0", selectedColor: "#FFA065", list: [{ "selectedIconPath": "/static/icon/icon_xcx_c_bar_home@3x.png", "iconPath": "/static/icon/icon_xcx_c_bar_home_gray@3x.png", "pagePath": "/pages/index/main", "text": "首页" }, { "selectedIconPath": "/static/icon/icon_xcx_c_bar_store_color@3x.png", "iconPath": "/static/icon/icon_xcx_c_bar_store_gary@3x.png", "pagePath": "/pages/store-list/main", "text": "找店" }, { "selectedIconPath": "/static/icon/icon_xcx_c_bar_mine_color@3x.png", "iconPath": "/static/icon/icon_xcx_c_bar_mine_gary@3x.png", "pagePath": "/pages/vip/main", "text": "会员" } ] }, methods: { onChange(e) { wx.switchTab({ url: this.data.list[e.detail].pagePath }); this.setData({ active: e.detail }); } } });
//json { "component": true, "usingComponents": { "van-tabbar": "/static/vant/tabbar/index", "van-tabbar-item": "/static/vant/tabbar-item/index" } }
onShow () { this.$root.$mp.page.getTabBar().setData({ active: 0 //对应页面的index }) }
new CopyWebpackPlugin([ { from: path.resolve(__dirname, '../src/custom-tab-bar'), to: path.resolve(config.build.assetsRoot, './custom-tab-bar'), ignore: ['.*'] } ])
这样打包之后,可以将src下的custom-tab-bar文件夹打包到dist目录下。