現膠囊上邊距 = 膠囊上邊界坐標 - 狀態欄高度
現膠囊右邊距 = 屏幕寬度 - 膠囊右邊界坐標
現膠囊下邊距 = 膠囊下邊界坐標 - 膠囊高度 - 狀態欄高度
導航欄高度 = 膠囊下邊界坐標 + 現膠囊下邊距
注意:膠囊下邊界坐標包含了 狀態欄、膠囊高度和狀態欄和膠囊高度之間的距離,因為膠囊是居中在導航欄里的 ,所以上邊距與下邊距應該一致,所以是 膠囊下邊界坐標 - 膠囊高度 - 狀態欄高度。
const app = getApp(); Component({ properties: { navbarData: { // 由父頁面傳遞的數據 type: Object, value: {}, observer: function (newVal, oldVal) { } } }, data: { haveBack: true, // 是否有返回按鈕,true 有 false 沒有 若從分享頁進入則為 false statusBarHeight: 0, // 狀態欄高度 navbarHeight: 0, // 頂部導航欄高度 navbarBtn: { // 膠囊位置信息 height: 0, width: 0, top: 0, bottom: 0, right: 0 } }, // 微信7.0.0支持wx.getMenuButtonBoundingClientRect()獲得膠囊按鈕高度 attached: function () { let statusBarHeight = app.globalData.systeminfo.statusBarHeight // 狀態欄高度 let headerPosi = app.globalData.headerBtnPosi // 膠囊位置信息 /** * wx.getMenuButtonBoundingClientRect() 坐標信息以屏幕左上角為原點 * 菜單按鍵寬度: 87 * 菜單按鍵高度: 32 * 菜單按鍵左邊界坐標: 278 * 菜單按鍵上邊界坐標: 26 * 菜單按鍵右邊界坐標: 365 * 菜單按鍵下邊界坐標: 58 */ let btnPosi = { // 膠囊實際位置,坐標信息不是左上角原點 height: headerPosi.height, width: headerPosi.width, // 膠囊top - 狀態欄高度 top: headerPosi.top - statusBarHeight, // 膠囊bottom - 膠囊height - 狀態欄height (現膠囊bottom 為距離導航欄底部的長度) bottom: headerPosi.bottom - headerPosi.height - statusBarHeight, // 屏幕寬度 - 膠囊right right: app.globalData.systeminfo.screenWidth - headerPosi.right } let haveBack; if (getCurrentPages().length === 1) { // 當只有一個頁面時 haveBack = false; } else { haveBack = true; } this.setData({ haveBack: haveBack, // 獲取是否是通過分享進入的小程序 statusBarHeight: statusBarHeight, navbarHeight: headerPosi.bottom + btnPosi.bottom, // 原膠囊bottom + 現膠囊bottom navbarBtn: btnPosi }) }, methods: { _goBack: function () { wx.navigateBack({ delta: 1 }); }, _goHome: function () { wx.switchTab({ url: '/pages/index/index', }); } } })
通過 getCurrentPages()
來判斷當前頁面是否從分享頁進入, 因為如果從分享頁進入頁面棧中應該只有一條數據,在跳轉到其他頁面時頁面棧的length則會增加 ,在其他頁面就會顯示出返回和首頁按鈕。
注意:微信7.0.0支持wx.getMenuButtonBoundingClientRect(),
如果想兼容低版本的微信,只能把導航欄的高度寫死,通過一些大佬的計算得出的高度:
'iPhone': 64,
'iPhone X': 88,
'android': 68
具體查看:
https://developers.weixin.qq.com/community/develop/doc/0006c012dc8028f04b070dd0551004
如果你使用 wx.getMenuButtonBoundingClientRect()
得到信息有小數 ,如下所示
{height: 24, width: 65.25, top: -0.5, bottom: -0.5, right: 101.25}
那么你可能是把開發工具中的視圖縮放了,還原成100%就正常了。
headerNavbar.wxss
.navbar-wrap { position: fixed; width: 100%; top: 0; z-index: 9999999; background-color: #3281FF; box-sizing: border-box; } .navbar-text { text-align: center; font-size: 36rpx; color: #fff; font-weight: 600; } .navbar-icon { position: fixed; display: flex; border-radius: 64rpx; border: 0.5px solid rgba(255,255,255, 0.3); box-sizing: border-box; } .navbar-icon image { height: 20px; width: 20px; padding: 5px 10px 10px; display: inline-block; overflow: hidden; } .navbar-icon view { height: 18px; border-left: 0.5px solid rgba(255,255,255, 0.3); margin-top: 6px; } .navbar-loading { background: #fff; text-align: center; }
引用組件頁面代碼
navigationStyle.json
{ "navigationStyle": "custom", "enablePullDownRefresh": true, "backgroundTextStyle": "light", "usingComponents": { "headerNavbar": "/components/headerNavbar/headerNavbar" } }
先在需要使用自定義導航欄的頁面json中添加navigationStyle:custom
enablePullDownRefresh: true 開啟下拉刷新
backgroundTextStyle: light是把loading的樣式改成白色,這樣就不會顯示出來loading的三個點
navigationStyle.wxml
<headernavbar navbar-data="{{nvabarData}}"></headernavbar> <view class="home-page"> <text> 上面是自定義導航欄↑↑↑ </text> <text> 下面是主體內容↓↓↓ </text> <navigator url="./testPage"> 跳轉到測試頁 </navigator> </view>
navigationStyle.js
Page({ data: { // 組件所需的參數 nvabarData: { showCapsule: 1, // 是否顯示左上角膠囊按鈕 1 顯示 0 不顯示 title: '組件列表' // 導航欄 中間的標題 } }, onPullDownRefresh() { setTimeout(() = >{ wx.stopPullDownRefresh(); // 停止下拉 }, 2000); }, })
注意:雖說這么做在小程序開發工具中看起來都是對的,得到的導航欄高度也是64px但是在真機上測試后,還是有偏差,在iphone8 plus上高度是60px。
可以通過這張圖明顯看到差了幾px,如果你是單獨幾個頁面使用自定義導航,細心的用戶可能會發現,但是基本不影響。如果是全局使用自定義導航,那就不存在這個問題了。
項目代碼:https://github.com/Shay0921/header-navbar.git
總結
以上所述是小編給大家介紹的詳解微信小程序膠囊按鈕返回|首頁自定義導航欄功能,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com