小程序如何實現(xiàn)中間帶加號的tabbar
前言
相信大家也看過很多app或者小程序的tabbar中間是一個突出加號,這個用自帶的組件無法實現(xiàn),需要我們手動寫一個tabbar的樣式來實現(xiàn)效果。我們一起看看吧!
首先新建一個tabbar的組件。

html代碼
注意點:需要判斷是否是iphonex系列的手機,這類手機需要設(shè)置高度。對于中間的加號突出的菜單,格外設(shè)置樣式。直接使用了navigator,省去了需要寫方法跳轉(zhuǎn)這一步驟。但是需要設(shè)置hover-class="none"屬性。避免點擊時候有陰影出現(xiàn)。
<view class="tabbar_box {{isIphoneX ? 'iphoneX-height':''}}">
<block wx:for="{{tabbar}}" wx:key="index">
<navigator wx:if="{{item.isSpecial}}" hover-class="none" url="{{item.pagePath}}" class="tabbar_nav {{item.selected?'selected' : ''}}" open-type="navigate">
<view class='special-wrapper'>+</view>
</navigator>
<navigator wx:else hover-class="none" url="{{item.pagePath}}" class="tabbar_nav {{item.selected?'selected' : ''}}" open-type="switchTab">
<image class="tabbar_icon" src="{{item.selected ? item.selectedIconPath : item.iconPath}}"></image>
<text>{{item.text}}</text>
</navigator>
</block>
</view>js代碼
定義tabbar的數(shù)據(jù),包括選中圖標(biāo)和未選中圖標(biāo),路徑url和菜單文本值。
data: {
isIphoneX: app.globalData.systemInfo.model.search('iPhone X') != -1 ? true : false,
tabbar: [{
"pagePath": "/teacher/pages/teach/classroom/classroom",
"iconPath": "/image/index.png",
"selectedIconPath": "/image/index-active.png",
"text": "首頁"
},
{
"pagePath": "/teacher/pages/teach/publish/publish",
"isSpecial": true,
},
{
"pagePath": "/teacher/pages/teach/mine/mine",
"iconPath": "/image/index.png",
"selectedIconPath": "/image/index-active.png",
"text": "我的"
}],
}在需要的頁面引入組件
在需要你用到tabbar的頁面引入組件
{
"usingComponents": {
"custom-tabbar": "../../components/common/custom-tabbar/custom-tabbar",
}
}適用不同頁面
可能會有不同的頁面需要展示不同tabbar的時候,可以通過傳參from,來判斷是哪個頁面引用的。從而展示不通的tabbar數(shù)據(jù)。就可以適用于其他需要tabbar的頁面。
<custom-tabbar from="classroom"></custom-tabbar>
設(shè)置tabbar選中狀態(tài)
通過判斷當(dāng)前路由和當(dāng)前tabbar數(shù)據(jù)的path,對比成功后設(shè)置selected屬性,實現(xiàn)選中效果。
setTabbar(){
let currentPages = getCurrentPages();
let pagePath = currentPages[currentPages.length - 1].route;
this.data.tabbar.forEach(item => {
item.selected = false;
if(item.pagePath.indexOf(pagePath) > -1){
item.selected = true;
}
})
this.setData({
tabbar: this.data.tabbar
});
}css代碼
樣式文件
.tabbar_box{
background-color: #fff;
display: flex;
justify-content: space-around;
position: fixed;
bottom: 0;
left: 0;
z-index: 10;
width: 100%;
height: 98rpx;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);
}
.tabbar_box.iphoneX-height{
padding-bottom: 66rpx;
}
.middle-wrapper{
position: absolute;
right: 310rpx;
bottom: 0;
background-color: #fff;
width: 120rpx;
height: 120rpx;
border-radius: 50%;
border-top: 2rpx solid #f2f2f3;
}
.middle-wrapper.iphoneX-height{
bottom: 66rpx;
}
.tabbar_nav{
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 20rpx;
height: 100%;
position: relative;
color: rgba(0, 0, 0, 0.45);
font-size: 24rpx;
}
.tabbar_icon{
width: 42rpx;
height: 42rpx;
margin-bottom: 10rpx;
}
.special-wrapper{
position: absolute;
top: -30rpx;
width: 74rpx;
height: 74rpx;
border-radius: 50%;
background-color: var(--main-font-color);
text-align: center;
box-sizing: border-box;
padding: 6rpx;
font-size: 54rpx;
line-height: 54rpx;
color: #fff;
}
.selected {
color: var(--main-font-color);
}最終效果

以上就是實現(xiàn)帶加號的tabbar的全部代碼了,記錄一下,溫故而知新!
總結(jié)
到此這篇關(guān)于小程序如何實現(xiàn)中間帶加號tabbar的文章就介紹到這了,更多相關(guān)小程序加號tabbar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js 判斷當(dāng)前時間是否處于某個一個時間段內(nèi)
這篇文章主要介紹了js 判斷當(dāng)前時間是否處于某個一個時間段內(nèi),使用 jutils - JavaScript常用函數(shù)庫的 isDuringDate 函數(shù)來實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
JavaScript+Canvas創(chuàng)建一個獨特的字符畫生成器
這篇文章主要介紹了如何使用 Canvas 和 JavaScript 創(chuàng)建一個獨特的字符畫生成器,通過此生成器,我們可以將圖片轉(zhuǎn)換為由字符構(gòu)成的作品,感興趣的可以了解下2024-01-01
通過js動態(tài)創(chuàng)建標(biāo)簽,并設(shè)置屬性方法
下面小編就為大家分享一篇通過js動態(tài)創(chuàng)建標(biāo)簽,并設(shè)置屬性方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

