小程序自定義導(dǎo)航欄組件開(kāi)發(fā),小程序自定義導(dǎo)航欄模板的實(shí)現(xiàn)
先說(shuō)一下我的需求吧,因?yàn)槲覀兊男〕绦蚴乔短琢藈ebview。 點(diǎn)擊webview里面的項(xiàng)目,點(diǎn)的層級(jí)太深,用戶后退很麻煩。然后pm又找了一個(gè)自定義的導(dǎo)航的小程序,發(fā)現(xiàn)別人可以,然后我就開(kāi)始研究。
小程序自定義導(dǎo)航欄開(kāi)發(fā)注意點(diǎn)與參考文檔
- 微信官方設(shè)計(jì)指導(dǎo)中關(guān)于膠囊按鈕的描述 由此推測(cè)膠囊寬度87pt=116px,設(shè)置之后,的確能產(chǎn)生較好的兼容性效果
- [2018-07-06]根據(jù)測(cè)試截圖發(fā)現(xiàn)微信膠囊寬度應(yīng)該在96px左右,還有待研究
- 社區(qū)相關(guān)Q&A:自定義標(biāo)題欄高度計(jì)算
- 使用時(shí)注意方法與屬性版本兼容性
-
完善之路:
- 開(kāi)一個(gè)項(xiàng)目采集設(shè)備的screenHeight,windowHeight,pixelRatio等信息到一個(gè)數(shù)據(jù)庫(kù)中
- 微信提供這樣一個(gè)數(shù)據(jù)庫(kù)便于計(jì)算,或者微信優(yōu)化自定義標(biāo)題欄(譬如通知欄可以改變顏色但不要算在自定義范圍內(nèi),給出膠囊寬高到通知欄距離到右側(cè)屏幕邊框距離等相關(guān)參數(shù))
非自定義導(dǎo)航欄高度怎么計(jì)算?
- wx.getSystemInfo 和 wx.getSystemInfoSync 獲取機(jī)器信息
- screenHeight - windowHeight 計(jì)算標(biāo)題欄高度
-
標(biāo)題欄高度
- 'iPhone': 64,
- 'iPhone X': 88,
-
'android': 68
—— 不完全統(tǒng)計(jì)(ip6 , ip5 , ip6p , ipx , 小米mix2 , 小米5等綜合了開(kāi)發(fā)工具提供的數(shù)據(jù)和真機(jī)數(shù)據(jù))所得
有了這些從網(wǎng)上找到的資料,還有名叫貓圈的小程序的例子。開(kāi)始代碼實(shí)現(xiàn)了。
配置
"window": {
"navigationBarBackgroundColor": "#fff",
"navigationBarTextStyle": "black",
"backgroundColor": "#fff",
"navigationStyle": "custom"
},復(fù)制代碼
這里navigationStyle 配置修改之后就只剩一個(gè)膠囊按鈕,也就意味著所有頁(yè)面的導(dǎo)航都的自定義實(shí)現(xiàn),你可以選擇模版或者組件來(lái)開(kāi)發(fā),這里我是選擇用的組件開(kāi)發(fā)。
然后定義導(dǎo)航的各個(gè)數(shù)值,我是在app。js 里面定義的
title_height: "64",
statusbarHeight: "24",
titleIcon_height: "32",
titleIcon_width: "87",
title_top: "24",
title_text: "xxx", // iphone X + 24
prefix: 24
復(fù)制代碼
組件
wxml的代碼
復(fù)制代碼 {{title_text}}
wxss的代碼
.title { width: 100%; background-color: #175dc6; box-sizing: border-box; position: fixed; transform: translateZ(0); z-index: 999990;}
.title_text { text-align: center; font-size: 37rpx; color: #fff; line-height: 44px;}
.title_icon { background-color: #175dc6; position: fixed; top: 54rpx; left: 16rpx; border-radius: 64rpx; box-sizing: border-box; border: 0.5px solid #ebe48e; display: flex;}
.title_icon image { height: 20px; width: 20px; padding-top: 5px; display: inline-block; overflow: hidden;}
.title_icon view { height: 18px; border-left: 1px solid #bfb973; margin-top: 6px;}
.floatL { float: left;}
.title_icon image:nth-of-type(1), .title_icon image:nth-of-type(2) { padding-right: 10px; padding-bottom: 10px; padding-left: 10px;}復(fù)制代碼
js的代碼
const app = getApp();
Component({
properties: {
isShow: { // 是否顯示后退按鈕
type: String,
value: "1"
},
isIndex: { // 是否主頁(yè)
type: Boolean,
value: false,
},
title_height: { //
type: String,
value: app.config.title_height,
},
titleIcon_height: {
type: String,
value: app.config.titleIcon_height,
},
titleIcon_width: {
type: String,
value: app.config.titleIcon_width,
},
statusbarHeight: {
type: String,
value: app.config.statusbarHeight,
},
title_top: {
type: String,
value: app.config.title_top,
},
title_text: {
type: String,
value: app.config.title_text,
},
},
methods: {
_goBack: function() {
wx.navigateBack({
delta: 1
});
},
_goHome: function() {
wx.switchTab({
url: "/pages/index/index"
});
}
}
})復(fù)制代碼
這樣組件就寫好了 只需要在你每個(gè)頁(yè)面里面用這個(gè)組件傳不同的值就可以了。
應(yīng)用
在首頁(yè)的wxml應(yīng)用。 json 文件里面的配置 "navigationBarTitleText": "啦啦啦", "navigationBarBackgroundColor": "#175dc6", "usingComponents": { "header": "/components/layout/header/header" }復(fù)制代碼第二部分:如何開(kāi)通一個(gè)小商店

立即掃碼關(guān)注

多門店/直營(yíng)/加盟連鎖管理系統(tǒng)