近期做個微信小程序項目,因項目設計需要,所以要做成自定義的tabBar才能實現設計效果圖,具體效果如下圖:
其中掃碼買單,這個按鈕效果,微信自帶的tabBar是無法實現的,其后嘗試了下custom-tab-bar 也是無法實現。
沒辦法了,既然微信的tabBar無法實現。那就自己弄個真-自定義tabBar來實現好了。
各位看官莫慌,下面就把解決方案放上來。首先先來講下解決方案的思路,然后再把代碼送上。
思路:
微信的tabBar無法實現,那么就放棄微信的tabBar,改用Component 來實現。把微信自帶的tabBar隱藏起來,用Component 做成偽tabBar並應該到頁面上。
1、把app.json下的 tabBar 給干掉。這樣在首頁就不會有tabBar顯示了。
2、寫Component 偽tabBar並應該到頁面上。
具體操作(代碼)如下:
app.json刪除 tabBar
寫Component 偽tabBar
// Componet/tabBar/tabBar.js Component({ /** * 組件的屬性列表 */ properties: { idx: { type: Number, value: 0 }, }, /** * 組件的初始數據 */ data: { tabBar: [{ "pagePath": "../../pages/index/index", "text": "首頁", "switchQr": false, "iconPath": "/image/home.svg", "selectedIconPath": "/image/home_sel.svg", }, { "pagePath": "", "text": "掃碼買單", "switchQr": true, "iconPath": "/image/saoma.svg", }, { "pagePath": "../../pages/user/user", "text": "我的", "switchQr": false, "iconPath": "/image/mine.svg", "selectedIconPath": "/image/mine_sel.svg", }, ] }, /** * 組件的方法列表 */ methods: { goToTab: function (e) { //如果點擊當前頁面則不進行跳轉 if (this.data.idx == e.currentTarget.dataset.index) { return false } wx.navigateTo({ url: e.currentTarget.dataset.url }) }, // 掃碼 switchQr() { // console.log('掃碼') }, } })
1 <!--Componet/tabBar/tabBar.wxml--> 2 <view class="tabBar"> 3 <view class="cont"> 4 <block wx:for="{{tabBar}}" wx:for-item="item" wx:key="tabBar"> 5 <view class="item {{item.switchQr?'switchQr':''}}" catchtap="{{!item.switchQr?'goToTab':'switchQr'}}" data-url="{{item.pagePath}}" data-index="{{index}}"> 6 <image class="ico" src="{{idx === index ? item.selectedIconPath : item.iconPath}}"></image> 7 <view class="txt {{idx === index ? 'selectedColor' : ''}}">{{item.text}} 8 </view> 9 </view> 10 </block> 11 </view> 12 </view>
1 /* Componet/tabBar/tabBar.wxss */ 2 3 .tabBar { 4 width: 100%; 5 position: fixed; 6 bottom: 0; 7 font-size: 20rpx; 8 color: #8A8A8A; 9 background: #fff; 10 /* border-top: 2rpx solid #eee; */ 11 box-shadow: 0rpx 1rpx 6rpx rgba(0,0,0,0.3); 12 } 13 14 .cont { 15 margin-top: 10rpx; 16 padding: 0; 17 z-index: 0; 18 height: calc(100rpx + env(safe-area-inset-bottom) / 2); 19 padding-bottom: calc(env(safe-area-inset-bottom) / 2); 20 display: flex; 21 } 22 23 .cont .item { 24 font-size: 24rpx; 25 position: relative; 26 flex: 1; 27 text-align: center; 28 padding: 0; 29 display: block; 30 height: auto; 31 line-height: 1; 32 margin: 0; 33 background-color: inherit; 34 overflow: initial; 35 justify-content: center; 36 align-items: center; 37 } 38 39 .cont .item .ico { 40 width: 46rpx; 41 height: 46rpx; 42 margin: auto 43 } 44 45 .cont .item .txt{ 46 margin-top: 8rpx; 47 color: #333; 48 } 49 50 .cont .switchQr .ico { 51 position: absolute; 52 width: 106rpx !important; 53 z-index: 2; 54 height: 106rpx !important; 55 border-radius: 50%; 56 font-size: 50rpx; 57 top: -50rpx; 58 left: 0; 59 right: 0; 60 margin: auto; 61 padding: 0; 62 63 } 64 .cont .switchQr .txt{ 65 margin-top: 56rpx; 66 } 67 68 .cont .item .selectedColor{ 69 color: #ff4e4e; 70 }
在index/user的頁面上應用。
1、要在index.json/user.json文件引用Componet
1 "usingComponents": { 2 "tabBar":"/Componet/tabBar/tabBar" 3 },
2、在頁面的引用Componet
1 <!-- index.wxml --> 2 <tabBar idx="0"></tabBar> 3 4 <!--user.wxml --> 5 <tabBar idx="2"></tabBar>