最近作的項目涉及相應的頁面顯示相應的頂部標題,因此就須要自定義頭部導航了。android
首先新建一個頂部導航公用組件topnav,導航高度怎麼計算?json
1.wx.getSystemInfo 和 wx.getSystemInfoSync 獲取機器信息 2.screenHeight - windowHeight 計算標題欄高度 3.標題欄高度 'iPhone': 64, 'iPhone X': 88, 'android': 68
app.js小程序
//app.js App({ onLaunch: function () { var that = this wx.getSystemInfo({ success: function (res) { that.globalData.platform = res.platform let totalTopHeight = 68 if (res.model.indexOf('iPhone X') !== -1) { totalTopHeight = 88 } else if (res.model.indexOf('iPhone') !== -1) { totalTopHeight = 64 } that.globalData.statusBarHeight = res.statusBarHeight that.globalData.titleBarHeight = totalTopHeight - res.statusBarHeight }, failure() { that.globalData.statusBarHeight = 0 that.globalData.titleBarHeight = 0 } }) })
還需更改配置app.json微信小程序
"window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#15ae67", "navigationBarTitleText": "微信小程序", "navigationBarTextStyle": "white", "navigationStyle": "custom" //自定義導航欄配置 }
topnav.wxml微信
<!--template.wxml--> <!-- 這是自定義組件的內部WXML結構 --> <view style="padding-top:{{someData.statusBarHeight+someData.titleBarHeight}}px"> <view class="hh-header"> <view class="status-bar" style="height:{{someData.statusBarHeight}}px"></view> <view class="title-bar" style="height:{{someData.titleBarHeight}}px"> <view wx:if="{{isShowBack=='true'}}" class='hh-nav-back ico-back' bindtap='goback'></view> <view wx:if="{{isShowBack=='false'}}" class='hh-nav-back'></view> <view class="hh-title">{{innerTitle}}</view> <view class="hh-nav-right"></view> </view> </view> </view> <slot></slot>
topnav.jsonapp
{ "component": true }
topnav.jsxss
const app = getApp() Component({ properties: { // 這裏定義了innerText屬性,屬性值能夠在組件使用時指定 innerTitle: { type: String, value: '頭部標題' }, isShowBack: { type: String, value: "true" } }, data: { // 這裏是一些組件內部數據 someData: { statusBarHeight: app.globalData.statusBarHeight, titleBarHeight: app.globalData.titleBarHeight } }, methods: { // 這裏是一個自定義方法 goback: function () { wx.navigateBack({ delta: 1, }) } } })
topnav.wxssflex
.ico-back{ width: 36rpx; height: 36rpx; background-size: contain; background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMDY3IDc5LjE1Nzc0NywgMjAxNS8wMy8zMC0yMzo0MDo0MiAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIDIwMTUgKFdpbmRvd3MpIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOjAwRjFBMThDOEU1NDExRThCQUMxRTFBRUNDRDNCMkJEIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOjAwRjFBMThEOEU1NDExRThCQUMxRTFBRUNDRDNCMkJEIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6MDBGMUExOEE4RTU0MTFFOEJBQzFFMUFFQ0NEM0IyQkQiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6MDBGMUExOEI4RTU0MTFFOEJBQzFFMUFFQ0NEM0IyQkQiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz7a0EjNAAAA80lEQVR42sTYOw7DIAwGYNxuPXHnTtygvUAuWalyE6WRMlBj7N/GEkOiCD4B4eHCzEUq3ljruPMe9fcstxcJOmGOqNNADcwR+SABs6SDOphrKqiHSZ3UGkwaSItJAY1gwkGjmFCQBRMGsmJCQB4MHOTFQEEIDAyEwkBASIwGRIrFr/XBZy03InpbQFJcFHW8Gu+2nnmUiFAM2daLzz/DVtHtaSc1DIX87SEo9MLoRkVsHS5U1OZqRkUeP0yo6APaMCrjCDuEyjrkq1GZ1yAVKvui2EXNuEqLqFnJBgk1LR3TQi08OWF1RqmyH8SAtB0yvgIMAGA41d9Fo4AZAAAAAElFTkSuQmCC); background-repeat: no-repeat; background-position-x: 20rpx; } .hh-header { position: fixed; top: 0; width: 100%; background-color: #14ae66; z-index: 99; } .hh-title{ font-size: 38rpx; text-align: center; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .title-bar { display: flex; justify-content: space-between; align-items: center; } .hh-nav-right{ width: 116px; } .hh-nav-back{ width: 116px; }
這樣組件就寫好了 只須要在你每一個頁面裏面用這個組件傳不一樣的值就能夠了。ui
index.wxmlthis
<component-topnav inner-title="{{title}}"></component-topnav> //顯示返回按鈕
index.js
//獲取應用實例 const app = getApp() Page({ data: { motto: 'Hello World', userInfo: {}, title:'自定義導航標題' }, })
index.json
{ "usingComponents": { "component-topnav": "/pages/component/topnav" } }