溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何實(shí)現(xiàn)微信小程序自定義navigationBar頂部導(dǎo)航欄適配所有機(jī)型

發(fā)布時(shí)間:2020-07-30 14:28:25 來(lái)源:億速云 閱讀:451 作者:小豬 欄目:web開(kāi)發(fā)

這篇文章主要為大家展示了如何實(shí)現(xiàn)微信小程序自定義navigationBar頂部導(dǎo)航欄適配所有機(jī)型,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。

前言

navigationBar相信大家都不陌生把?今天我們就來(lái)說(shuō)說(shuō)自定義navigationBar,把它改變成我們想要的樣子(搜索框+膠囊、搜索框+返回按鈕+膠囊等)。

思路

  • 隱藏原生樣式
  • 獲取膠囊按鈕、狀態(tài)欄相關(guān)數(shù)據(jù)以供后續(xù)計(jì)算
  • 根據(jù)不同機(jī)型計(jì)算出該機(jī)型的導(dǎo)航欄高度,進(jìn)行適配
  • 編寫(xiě)新的導(dǎo)航欄
  • 引用到頁(yè)面

正文

一、隱藏原生的navigationBar

window全局配置里有個(gè)參數(shù):navigationStyle(導(dǎo)航欄樣式),default=默認(rèn)樣式,custom=自定義樣式。

"window": {
	"navigationStyle": "custom"
}

讓我們看看隱藏后的效果:

如何實(shí)現(xiàn)微信小程序自定義navigationBar頂部導(dǎo)航欄適配所有機(jī)型

可以看到原生的navigationBar已經(jīng)消失了,剩下孤零零的膠囊按鈕,膠囊按鈕是無(wú)法隱藏的。

二、準(zhǔn)備工作

1.獲取膠囊按鈕的布局位置信息

我們用wx.getMenuButtonBoundingClientRect() 【官方文檔】 獲取膠囊按鈕的布局位置信息,坐標(biāo)信息以屏幕左上角為原點(diǎn):

const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
widthheighttoprightbottomleft
寬度高度上邊界坐標(biāo)右邊界坐標(biāo)下邊界坐標(biāo)左邊界坐標(biāo)

下面是官方給的示意圖,方便大家理解幾個(gè)坐標(biāo)。

如何實(shí)現(xiàn)微信小程序自定義navigationBar頂部導(dǎo)航欄適配所有機(jī)型

2.獲取系統(tǒng)信息

用wx.getSystemInfoSync() 【官方文檔】 獲取系統(tǒng)信息,里面有個(gè)參數(shù):statusBarHeight(狀態(tài)欄高度),是我們后面計(jì)算整個(gè)導(dǎo)航欄的高度需要用到的。

const systemInfo = wx.getSystemInfoSync();

三、計(jì)算公式

我們先要知道導(dǎo)航欄高度是怎么組成的, 計(jì)算公式: 導(dǎo)航欄高度 = 狀態(tài)欄到膠囊的間距(膠囊距上距離-狀態(tài)欄高度) * 2 + 膠囊高度 + 狀態(tài)欄高度 。

實(shí)例 【源碼下載】

自定義導(dǎo)航欄會(huì)應(yīng)用到多個(gè)、甚至全部頁(yè)面,所以封裝成組件,方便調(diào)用;下面是我寫(xiě)的一個(gè)簡(jiǎn)單例子:

app.js

App({
 onLaunch: function(options) {
  const that = this;
  // 獲取系統(tǒng)信息
  const systemInfo = wx.getSystemInfoSync();
  // 膠囊按鈕位置信息
  const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  // 導(dǎo)航欄高度 = 狀態(tài)欄到膠囊的間距(膠囊距上距離-狀態(tài)欄高度) * 2 + 膠囊高度 + 狀態(tài)欄高度
  that.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight;
  that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  that.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
  that.globalData.menuHeight = menuButtonInfo.height;
 },
 // 數(shù)據(jù)都是根據(jù)當(dāng)前機(jī)型進(jìn)行計(jì)算,這樣的方式兼容大部分機(jī)器
 globalData: {
  navBarHeight: 0, // 導(dǎo)航欄高度
  menuRight: 0, // 膠囊距右方間距(方保持左、右間距一致)
  menuBotton: 0, // 膠囊距底部間距(保持底部間距一致)
  menuHeight: 0, // 膠囊高度(自定義內(nèi)容可與膠囊高度保證一致)
 }
})

app.json

{
 "pages": [
  "pages/index/index"
 ],
 "window": {
  "navigationStyle": "custom"
 },
 "sitemapLocation": "sitemap.json"
}

下面為組件代碼: /components/navigation-bar/navigation-bar.wxml

<!-- 自定義頂部欄 -->
<view class="nav-bar" >
 <input class="search" placeholder="輸入關(guān)鍵詞!" ></input>
</view>

<!-- 
 內(nèi)容區(qū)域:
 自定義頂部欄用的fixed定位,會(huì)遮蓋到下面內(nèi)容,注意設(shè)置好間距
-->
<view class="content" ></view>

/components/navigation-bar/navigation-bar.json

{
 "component": true
}

/components/navigation-bar/navigation-bar.js

const app = getApp()
Component({
 properties: {
  // defaultData(父頁(yè)面?zhèn)鬟f的數(shù)據(jù)-就是引用組件的頁(yè)面)
  defaultData: {
   type: Object,
   value: {
    title: "我是默認(rèn)標(biāo)題"
   },
   observer: function(newVal, oldVal) {}
  }
 },
 data: {
  navBarHeight: app.globalData.navBarHeight,
  menuRight: app.globalData.menuRight,
  menuBotton: app.globalData.menuBotton,
  menuHeight: app.globalData.menuHeight,
 },
 attached: function() {},
 methods: {}
})

/components/navigation-bar/navigation-bar.wxss

.nav-bar{ position: fixed; width: 100%; top: 0; color: #fff; background: #000;}
.nav-bar .search{ width: 60%; color: #333; font-size: 14px; background: #fff; position: absolute; border-radius: 50px; background: #ddd; padding-left: 14px;}

以下是調(diào)用頁(yè)面的代碼,也就是引用組件的頁(yè)面: /pages/index/index.wxml

navigation-bar default-data="{{defaultData}}"></navigation-bar>

/pages/index/index.json

{
 "usingComponents": {
  "navigation-bar": "/components/navigation-bar/navigation-bar"
 }
}

/pages/index/index.js

const app = getApp();
Page({
 data: {
  // 組件參數(shù)設(shè)置,傳遞到組件
  defaultData: {
   title: "我的主頁(yè)", // 導(dǎo)航欄標(biāo)題
  }
 },
 onLoad() {
  console.log(this.data.height)
 }
})

效果圖:

如何實(shí)現(xiàn)微信小程序自定義navigationBar頂部導(dǎo)航欄適配所有機(jī)型

好了,以上就是全部代碼了,大家可以文中復(fù)制代碼,也可以 【下載源碼】

,直接到開(kāi)發(fā)者工具里運(yùn)行,記得appid用自己的或者測(cè)試哦!

下面附幾張其它小程序的效果圖,大家也可以嘗試照著做:

如何實(shí)現(xiàn)微信小程序自定義navigationBar頂部導(dǎo)航欄適配所有機(jī)型 如何實(shí)現(xiàn)微信小程序自定義navigationBar頂部導(dǎo)航欄適配所有機(jī)型

以上就是關(guān)于如何實(shí)現(xiàn)微信小程序自定義navigationBar頂部導(dǎo)航欄適配所有機(jī)型的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI