微信小程序?qū)崿F(xiàn)自定義導(dǎo)航欄
本文實(shí)例為大家分享了微信小程序自定義導(dǎo)航欄的具體代碼,供大家參考,具體內(nèi)容如下
1、要實(shí)現(xiàn)自定義導(dǎo)航欄,首先得在全局進(jìn)行相關(guān)配置
app.json頁面
"window": {
? ? ...
? ? "navigationStyle": "custom"
},根據(jù)微信小程序官方文檔的說法,只有客戶端7.0.0以上版本才支持局部頁面實(shí)現(xiàn)自定義導(dǎo)航欄,7.0.0以下版本只支持全體頁面的自定義導(dǎo)航欄,自己項(xiàng)目里采用的是就是這種

app.js頁面
App({
? onLaunch: function(options) {
? ? var _this = this;
? ??
? ? //自定義導(dǎo)航欄 獲取設(shè)備頂部窗口的高度(不同設(shè)備窗口高度不一樣,根據(jù)這個(gè)來設(shè)置自定義導(dǎo)航欄的高度)
? ? wx.getSystemInfo({
? ? ? success: (res) => {
? ? ? ? // 基礎(chǔ)庫 2.1.0 開始支持wx.getMenuButtonBoundingClientRect(),低版本需要適配
? ? ? ? let custom = wx.getMenuButtonBoundingClientRect()
? ? ? ? // console.log('狀態(tài)欄高度',res.statusBarHeight)
? ? ? ? // console.log('右上角膠囊按鈕的高度', custom.height)
? ? ? ? // console.log('右上角膠囊按鈕的上邊界坐標(biāo)', custom.top)
? ? ? ? // console.log('右上角膠囊按鈕的下邊界坐標(biāo)', custom.bottom)
? ? ? ? _this.globalData.statusBarHeight = res.statusBarHeight
? ? ? ? _this.globalData.navBarHeight = custom.height + (custom.top - res.statusBarHeight) * 2
? ? ? }
? ? })
? },
? globalData: { // 全局?jǐn)?shù)據(jù)
? ? statusBarHeight: 0,
? ? navBarHeight: 0,
? },
})2、創(chuàng)建自定義導(dǎo)航欄組件,組件目錄為 /components/navbar
navbar.wxml頁面
<!-- 默認(rèn)為黑色的返回鍵-->
<view class='nav-wrap nav-bgc-class' style='height: {{statusBarHeight + navBarHeight}}px;'>
? <!-- ?左上角的返回按鈕 其中wx:if='{{navbarData.showCapsule}}' 是控制左上角按鈕的顯示隱藏,1為顯示,0為隱藏 -->
? <view class='nav-capsule' style='margin-top: {{statusBarHeight}}px; height: {{navBarHeight}}px;' wx:if='{{navbarData.showCapsule}}' bindtap='_navback'>
? ? <image class='back-pre ex-back-pre' src='{{navbarData.backSrc || "/img/back4.png"}}' mode='aspectFill'></image>
? </view>
? ? <!-- ?中間的標(biāo)題 -->
? <view class='nav-title nav-title-class' style='margin-top: {{statusBarHeight}}px; height: {{navBarHeight}}px;'>{{navbarData.title}}</view>
</view>navbar.js頁面
const app = getApp()
Component({
? // multipleSlots 為組件開啟多插槽模式
? options: {
? ? multipleSlots: true,
? },
? // externalClasses 為組件指定多個(gè)外部樣式類
? externalClasses: ['nav-bgc-class', 'nav-title-class','ex-back-pre'],
? // properties 組件用來儲(chǔ)存外部數(shù)據(jù)
? properties: {
? ? navbarData: { //navbarData ? 由父頁面?zhèn)鬟f的數(shù)據(jù),變量名字自命名
? ? ? type: Object,
? ? ? value: {},
? ? ? observer: function (newVal, oldVal) { }
? ? },
? },
? // 組件用來儲(chǔ)存內(nèi)部私有數(shù)據(jù)
? data: {
? ? // 自定義導(dǎo)航欄的高度
? ? statusBarHeight: app.globalData.statusBarHeight,
? ? navBarHeight: app.globalData.navBarHeight
? },
? // attached函數(shù) 當(dāng)組件進(jìn)入頁面節(jié)點(diǎn)樹時(shí)觸發(fā),可以使用setData,絕大多數(shù)初始化工作在這個(gè)時(shí)機(jī)進(jìn)行
? attached: function () {},
? // methods對(duì)象 定義組件內(nèi)的各種方法
? methods: {
? ? // 返回鍵,觸發(fā)自定義事件,將返回的事件交給父級(jí)頁面來分情況定義
? ? _navback() {
? ? ? this.triggerEvent('goBack')
? ? }
? }
})navbar.json頁面
{
? "component": true
}navbar.wxss頁面
/* 頂部固定定位 ? 標(biāo)題要居中 ? 自定義按鈕和標(biāo)題要和右邊微信原生的膠囊上下對(duì)齊 */
.nav-wrap {
? position: fixed;
? width: 750rpx;
? top: 0;
? left: 0;
? right: 0;
? background: #f4f4f4;
? /* background-color: pink; */
? z-index: 9999999;
? transform: translate3d(0, 0, 0);
}
/* 返回鍵 */
.nav-capsule {
? width: 140rpx;
? /* background-color: skyblue; */
? /* 讓里面的圖片元素垂直居中 */
? display: flex;
? align-items: center;
}
.back-pre {
? width: 100rpx;
? height: 68rpx;
? /* background-color: green; */
}
/* 標(biāo)題 */
.nav-title {
? position: absolute;
? left: 0;
? right: 0;
? bottom: 0;
? max-width: 400rpx;
? margin: auto;
? /* background-color: deeppink; */
? /* 水平垂直居中 */
? display: flex;
? align-items: center;
? justify-content: space-around;
? /* 超出部分省略號(hào)顯示 */
? overflow: hidden;
? text-overflow: ellipsis;
? white-space: nowrap;
? /* 字體設(shè)置 */
? color: #111111;
? font-size: 32rpx;
? font-weight: 500;
}3、在其它頁面使用自定義導(dǎo)航欄組件(需要修改默認(rèn)樣式)
transation_detail.json頁面
{
? "usingComponents": {
? ? "nav-bar": "/components/navbar/navbar",
? }
}transation_detail.wxml頁面
<!-- 引入自定義導(dǎo)航欄組件 -->
<nav-bar bind:goBack="_goBack" nav-bgc-class="ex-nav-bgc-class" nav-title-class="ex-nav-title-class" ex-back-pre="ex-back-pre" navbar-data='{{nvabarData}}'>
</nav-bar>transation_detail.wxss頁面
/* 需要從外部傳入導(dǎo)航欄組件的樣式 */
/* 導(dǎo)航欄背景色 */
.ex-nav-bgc-class {
? background: transparent !important;
}
/* 導(dǎo)航欄標(biāo)題顏色 */
.ex-nav-title-class {
? color: #fff !important;
}
/* 導(dǎo)航欄返回鍵樣式 */
.ex-back-pre {
? width: 60rpx!important;
? height: 60rpx!important;
? margin-top: 4rpx!important;
? padding: 40rpx!important;
}transation_detail.js頁面
const app = getApp()
Page({
? /**
? ?* 頁面的初始數(shù)據(jù)
? ?*/
? data: {
? ? // 自定義導(dǎo)航欄需要的參數(shù)
? ? nvabarData: {
? ? ? showCapsule: 1, //是否顯示左上角圖標(biāo) ? 1表示顯示 ? ?0表示不顯示
? ? ? title: '', //導(dǎo)航欄 中間的標(biāo)題
? ? ? backSrc: '/img/back3.png'?? ?// 返回鍵的樣式
? ? },
? ? // 此頁面 頁面內(nèi)容距最頂部的距離
? ? height: app.globalData.statusBarHeight + app.globalData.navBarHeight,
? },
? /**
? ?* 生命周期函數(shù)--監(jiān)聽頁面加載
? ?*/
? onLoad: function(options) {},
??
? // 點(diǎn)擊 頂部返回鍵時(shí) 要返回的頁面
? _goBack() {
? ? wx.switchTab({
? ? ? url: '/pages/mer_index/mer_index',
? ? })
? },
})4、自定義導(dǎo)航欄可以不傳樣式,這時(shí)會(huì)采用默認(rèn)樣式
order.wxml頁面
<!-- 引入自定義導(dǎo)航欄組件 -->
<nav-bar bind:goBack="_goBack" navbar-data='{{nvabarData}}'></nav-bar>order.js頁面
const app = getApp()
Page({
? /**
? ?* 頁面的初始數(shù)據(jù)
? ?*/
? data: {
? ? // 自定義導(dǎo)航欄需要的參數(shù)
? ? nvabarData: {
? ? ? showCapsule: 1, //是否顯示左上角圖標(biāo) ? 1表示顯示 ? ?0表示不顯示
? ? ? title: '現(xiàn)有倉單', //導(dǎo)航欄 中間的標(biāo)題
? ? },
? ? // 此頁面 頁面內(nèi)容距最頂部的距離
height: app.globalData.statusBarHeight + app.globalData.navBarHeight,以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序自定義頂部導(dǎo)航欄并適配不同機(jī)型實(shí)例詳解
- 微信小程序動(dòng)態(tài)設(shè)置導(dǎo)航欄標(biāo)題的實(shí)現(xiàn)步驟
- 小程序自定義tabbar導(dǎo)航欄及動(dòng)態(tài)控制tabbar功能實(shí)現(xiàn)方法(uniapp)
- 微信小程序使用uni-app實(shí)現(xiàn)首頁搜索框?qū)Ш綑诠δ茉斀?/a>
- uniapp小程序配置tabbar底部導(dǎo)航欄實(shí)戰(zhàn)指南
- 微信小程序自定義漸變的tabbar導(dǎo)航欄功能
- uniapp開發(fā)微信小程序自定義頂部導(dǎo)航欄功能實(shí)例
- 微信小程序?qū)崿F(xiàn)側(cè)邊導(dǎo)航欄
- uniapp微信小程序自定義導(dǎo)航欄的全過程
- 微信小程序自定義導(dǎo)航欄功能的實(shí)現(xiàn)
相關(guān)文章
在window.setTimeout方法中傳送對(duì)象
setTimeout方法是js中的延時(shí)方法,很多js的bug,只需要使用該方法延時(shí)一下,就會(huì)自動(dòng)解決了,簡直就是萬能藥方,也是我比較喜歡使用的最后手段。2006-12-12
利用10行js代碼實(shí)現(xiàn)上下滾動(dòng)公告效果
這篇文章主要給大家介紹了關(guān)于利用10行js代碼實(shí)現(xiàn)滾動(dòng)公告效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。2017-12-12
跟我學(xué)習(xí)javascript創(chuàng)建對(duì)象(類)的8種方法
跟我學(xué)習(xí)javascript創(chuàng)建對(duì)象(類)的8種方法,每一種方法都有詳細(xì)的介紹,不知道javascript如何創(chuàng)建對(duì)象的朋友,不要錯(cuò)過這篇文章。2015-11-11
頁面下沉抖動(dòng)效果-網(wǎng)站HTTP連接沒有效果-PC上有效果
頁面下沉抖動(dòng)效果實(shí)現(xiàn)代碼,代碼少,功能還可以2008-05-05
利用JavaScript緩存遠(yuǎn)程竊取Wi-Fi密碼的思路詳解
這篇文章主要介紹了利用JavaScript緩存遠(yuǎn)程竊取Wi-Fi密碼的方法,本文通過實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
JS自定義滾動(dòng)條效果簡單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了JS自定義滾動(dòng)條效果的簡單實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
詳解JavaScript什么情況下不建議使用箭頭函數(shù)
箭頭函數(shù)作為ES6新增的語法,在使用時(shí)不僅能使得代碼更加簡潔,而且在某些場景避免this指向問題。但是箭頭函數(shù)不是萬能的,也有自己的缺點(diǎn)以及不適用的場景,本文總結(jié)了JavaScript什么情況下不建議使用箭頭函數(shù),感興趣的可以了解一下2022-06-06
微信小程序保存圖片到相冊(cè)權(quán)限設(shè)置
這篇文章主要為大家詳細(xì)介紹了微信小程序保存圖片到相冊(cè)權(quán)限設(shè)置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04

