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

html代碼
注意點(diǎn):需要判斷是否是iphonex系列的手機(jī),這類手機(jī)需要設(shè)置高度。對(duì)于中間的加號(hào)突出的菜單,格外設(shè)置樣式。直接使用了navigator,省去了需要寫(xiě)方法跳轉(zhuǎn)這一步驟。但是需要設(shè)置hover-class="none"屬性。避免點(diǎn)擊時(shí)候有陰影出現(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": "首頁(yè)"
},
{
"pagePath": "/teacher/pages/teach/publish/publish",
"isSpecial": true,
},
{
"pagePath": "/teacher/pages/teach/mine/mine",
"iconPath": "/image/index.png",
"selectedIconPath": "/image/index-active.png",
"text": "我的"
}],
}在需要的頁(yè)面引入組件
在需要你用到tabbar的頁(yè)面引入組件
{
"usingComponents": {
"custom-tabbar": "../../components/common/custom-tabbar/custom-tabbar",
}
}適用不同頁(yè)面
可能會(huì)有不同的頁(yè)面需要展示不同tabbar的時(shí)候,可以通過(guò)傳參from,來(lái)判斷是哪個(gè)頁(yè)面引用的。從而展示不通的tabbar數(shù)據(jù)。就可以適用于其他需要tabbar的頁(yè)面。
<custom-tabbar from="classroom"></custom-tabbar>
設(shè)置tabbar選中狀態(tài)
通過(guò)判斷當(dāng)前路由和當(dāng)前tabbar數(shù)據(jù)的path,對(duì)比成功后設(shè)置selected屬性,實(shí)現(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);
}最終效果

以上就是實(shí)現(xiàn)帶加號(hào)的tabbar的全部代碼了,記錄一下,溫故而知新!
總結(jié)
到此這篇關(guān)于小程序如何實(shí)現(xiàn)中間帶加號(hào)tabbar的文章就介紹到這了,更多相關(guān)小程序加號(hào)tabbar內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript的var與let,const之間的區(qū)別詳解
這篇文章主要為大家介紹了?javascript的var與let,const之間的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12
JavaScript時(shí)間復(fù)雜度和空間復(fù)雜度
這篇文章主要介紹了JavaScript時(shí)間復(fù)雜度和空間復(fù)雜度,時(shí)間復(fù)雜度和空間復(fù)雜度是衡量一個(gè)算法是否優(yōu)秀的標(biāo)準(zhǔn),通常我們比較兩個(gè)算法時(shí)會(huì)用預(yù)先估算和事后統(tǒng)計(jì),下文詳細(xì)介紹,需要的朋友可以參考一下2022-07-07
ES6學(xué)習(xí)筆記之正則表達(dá)式和字符串正則方法分析
這篇文章主要介紹了ES6學(xué)習(xí)筆記之正則表達(dá)式和字符串正則方法,結(jié)合實(shí)例形式對(duì)比分析了ES5與ES6正則操作的常用函數(shù)功能與用法區(qū)別,需要的朋友可以參考下2017-04-04
頁(yè)面js遇到亂碼問(wèn)題的解決方法是和無(wú)法轉(zhuǎn)碼的情況
在老項(xiàng)目里加些js文件和老項(xiàng)目的編碼格式不一致出現(xiàn)亂碼,由于兩個(gè)文件都不能轉(zhuǎn)格式,于是百度個(gè)不錯(cuò)的方法在此與大家分享下2014-04-04
使用JS獲取input file的路徑C:\fakepath\問(wèn)題及解決方法
這篇文章主要介紹了使用JS獲取input file的路徑C:\fakepath\問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
JavaScript?算法實(shí)現(xiàn)復(fù)寫(xiě)0雙指針解法
這篇文章主要為大家介紹了JavaScript?算法?復(fù)寫(xiě)0雙指針解法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
JavaScript二叉搜索樹(shù)構(gòu)建操作詳解
這篇文章主要介紹了JavaScript二叉搜索樹(shù)構(gòu)建操作詳解,文章圍繞主題展開(kāi)二叉搜索樹(shù)的性質(zhì)以及二叉搜索樹(shù)的構(gòu)建、查找和刪除等內(nèi)容,需要的小伙伴可以參考一下2022-07-07

