注冊登錄

小程序自定義導(dǎo)航欄組件開發(fā),小程序自定義導(dǎo)航欄模板的實(shí)現(xiàn)

2018-09-20
導(dǎo)讀:先說一下我的需求吧,因?yàn)槲覀兊男〕绦蚴乔短琢藈ebview。 點(diǎn)擊webview里面的項(xiàng)目,點(diǎn)的層級太深,用戶后退很麻煩。然后pm又找了一個自定義的導(dǎo)航的小程序,發(fā)現(xiàn)別人可以,然后我就...

先說一下我的需求吧,因?yàn)槲覀兊男〕绦蚴乔短琢藈ebview。 點(diǎn)擊webview里面的項(xiàng)目,點(diǎn)的層級太深,用戶后退很麻煩。然后pm又找了一個自定義的導(dǎo)航的小程序,發(fā)現(xiàn)別人可以,然后我就開始研究。

小程序自定義導(dǎo)航欄開發(fā)注意點(diǎn)與參考文檔

 

  • 微信官方設(shè)計(jì)指導(dǎo)中關(guān)于膠囊按鈕的描述 由此推測膠囊寬度87pt=116px,設(shè)置之后,的確能產(chǎn)生較好的兼容性效果
  • [2018-07-06]根據(jù)測試截圖發(fā)現(xiàn)微信膠囊寬度應(yīng)該在96px左右,還有待研究
  • 社區(qū)相關(guān)Q&A:自定義標(biāo)題欄高度計(jì)算
  • 使用時注意方法與屬性版本兼容性
  • 完善之路:
    1. 開一個項(xiàng)目采集設(shè)備的screenHeight,windowHeight,pixelRatio等信息到一個數(shù)據(jù)庫中
    2. 微信提供這樣一個數(shù)據(jù)庫便于計(jì)算,或者微信優(yōu)化自定義標(biāo)題欄(譬如通知欄可以改變顏色但不要算在自定義范圍內(nèi),給出膠囊寬高到通知欄距離到右側(cè)屏幕邊框距離等相關(guān)參數(shù))

非自定義導(dǎo)航欄高度怎么計(jì)算?

  1. wx.getSystemInfo 和 wx.getSystemInfoSync 獲取機(jī)器信息
  2. screenHeight - windowHeight 計(jì)算標(biāo)題欄高度
  3. 標(biāo)題欄高度
    • 'iPhone': 64,
    • 'iPhone X': 88,
    • 'android': 68

      —— 不完全統(tǒng)計(jì)(ip6 , ip5 , ip6p , ipx , 小米mix2 , 小米5等綜合了開發(fā)工具提供的數(shù)據(jù)和真機(jī)數(shù)據(jù))所得

 

有了這些從網(wǎng)上找到的資料,還有名叫貓圈的小程序的例子。開始代碼實(shí)現(xiàn)了。

配置

"window": {        
        "navigationBarBackgroundColor": "#fff",        
        "navigationBarTextStyle": "black",       
        "backgroundColor": "#fff",        
        "navigationStyle": "custom"    
    },復(fù)制代碼

這里navigationStyle 配置修改之后就只剩一個膠囊按鈕,也就意味著所有頁面的導(dǎo)航都的自定義實(shí)現(xiàn),你可以選擇模版或者組件來開發(fā),這里我是選擇用的組件開發(fā)。

然后定義導(dǎo)航的各個數(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的代碼

 
    {{title_text}}    
              
復(fù)制代碼

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: { // 是否主頁            
            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ù)制代碼

這樣組件就寫好了 只需要在你每個頁面里面用這個組件傳不同的值就可以了。

應(yīng)用

在首頁的wxml應(yīng)用。 json 文件里面的配置 "navigationBarTitleText": "啦啦啦", "navigationBarBackgroundColor": "#175dc6", "usingComponents": { "header": "/components/layout/header/header" }復(fù)制代碼
重磅推薦:小程序開店目錄

第一部分:小商店是什么

第二部分:如何開通一個小商店

第三部分:如何登錄小商店

第四部分:開店任務(wù)常見問題

第五部分:小商店可以賣什么

第六部分:HiShop小程序特色功能

第七部分:小程序直播

第八部分:小程序收貨/物流

第九部分:小程序怎么結(jié)算

第十部分:小程序客服

第十一部分:電商創(chuàng)業(yè)

第十二部分:小程序游戲開發(fā)

電話咨詢 微信咨詢 預(yù)約演示 0元開店