微信小程序?qū)崿F(xiàn)自定義導(dǎo)航欄
本文實例為大家分享了微信小程序自定義導(dǎo)航欄的具體代碼,供大家參考,具體內(nèi)容如下
1、要實現(xiàn)自定義導(dǎo)航欄,首先得在全局進行相關(guān)配置
app.json頁面
"window": { ? ? ... ? ? "navigationStyle": "custom" },
根據(jù)微信小程序官方文檔的說法,只有客戶端7.0.0以上版本才支持局部頁面實現(xiàn)自定義導(dǎo)航欄,7.0.0以下版本只支持全體頁面的自定義導(dǎo)航欄,自己項目里采用的是就是這種
app.js頁面
App({ ? onLaunch: function(options) { ? ? var _this = this; ? ?? ? ? //自定義導(dǎo)航欄 獲取設(shè)備頂部窗口的高度(不同設(shè)備窗口高度不一樣,根據(jù)這個來設(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('右上角膠囊按鈕的上邊界坐標', custom.top) ? ? ? ? // console.log('右上角膠囊按鈕的下邊界坐標', custom.bottom) ? ? ? ? _this.globalData.statusBarHeight = res.statusBarHeight ? ? ? ? _this.globalData.navBarHeight = custom.height + (custom.top - res.statusBarHeight) * 2 ? ? ? } ? ? }) ? }, ? globalData: { // 全局數(shù)據(jù) ? ? statusBarHeight: 0, ? ? navBarHeight: 0, ? }, })
2、創(chuàng)建自定義導(dǎo)航欄組件,組件目錄為 /components/navbar
navbar.wxml頁面
<!-- 默認為黑色的返回鍵--> <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> ? ? <!-- ?中間的標題 --> ? <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 為組件指定多個外部樣式類 ? externalClasses: ['nav-bgc-class', 'nav-title-class','ex-back-pre'], ? // properties 組件用來儲存外部數(shù)據(jù) ? properties: { ? ? navbarData: { //navbarData ? 由父頁面?zhèn)鬟f的數(shù)據(jù),變量名字自命名 ? ? ? type: Object, ? ? ? value: {}, ? ? ? observer: function (newVal, oldVal) { } ? ? }, ? }, ? // 組件用來儲存內(nèi)部私有數(shù)據(jù) ? data: { ? ? // 自定義導(dǎo)航欄的高度 ? ? statusBarHeight: app.globalData.statusBarHeight, ? ? navBarHeight: app.globalData.navBarHeight ? }, ? // attached函數(shù) 當組件進入頁面節(jié)點樹時觸發(fā),可以使用setData,絕大多數(shù)初始化工作在這個時機進行 ? attached: function () {}, ? // methods對象 定義組件內(nèi)的各種方法 ? methods: { ? ? // 返回鍵,觸發(fā)自定義事件,將返回的事件交給父級頁面來分情況定義 ? ? _navback() { ? ? ? this.triggerEvent('goBack') ? ? } ? } })
navbar.json頁面
{ ? "component": true }
navbar.wxss頁面
/* 頂部固定定位 ? 標題要居中 ? 自定義按鈕和標題要和右邊微信原生的膠囊上下對齊 */ .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; */ } /* 標題 */ .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; ? /* 超出部分省略號顯示 */ ? overflow: hidden; ? text-overflow: ellipsis; ? white-space: nowrap; ? /* 字體設(shè)置 */ ? color: #111111; ? font-size: 32rpx; ? font-weight: 500; }
3、在其它頁面使用自定義導(dǎo)航欄組件(需要修改默認樣式)
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)航欄標題顏色 */ .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, //是否顯示左上角圖標 ? 1表示顯示 ? ?0表示不顯示 ? ? ? title: '', //導(dǎo)航欄 中間的標題 ? ? ? backSrc: '/img/back3.png'?? ?// 返回鍵的樣式 ? ? }, ? ? // 此頁面 頁面內(nèi)容距最頂部的距離 ? ? height: app.globalData.statusBarHeight + app.globalData.navBarHeight, ? }, ? /** ? ?* 生命周期函數(shù)--監(jiān)聽頁面加載 ? ?*/ ? onLoad: function(options) {}, ?? ? // 點擊 頂部返回鍵時 要返回的頁面 ? _goBack() { ? ? wx.switchTab({ ? ? ? url: '/pages/mer_index/mer_index', ? ? }) ? }, })
4、自定義導(dǎo)航欄可以不傳樣式,這時會采用默認樣式
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, //是否顯示左上角圖標 ? 1表示顯示 ? ?0表示不顯示 ? ? ? title: '現(xiàn)有倉單', //導(dǎo)航欄 中間的標題 ? ? }, ? ? // 此頁面 頁面內(nèi)容距最頂部的距離 height: app.globalData.statusBarHeight + app.globalData.navBarHeight,
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序自定義頂部導(dǎo)航欄并適配不同機型實例詳解
- 微信小程序動態(tài)設(shè)置導(dǎo)航欄標題的實現(xiàn)步驟
- 小程序自定義tabbar導(dǎo)航欄及動態(tài)控制tabbar功能實現(xiàn)方法(uniapp)
- 微信小程序使用uni-app實現(xiàn)首頁搜索框?qū)Ш綑诠δ茉斀?/a>
- uniapp小程序配置tabbar底部導(dǎo)航欄實戰(zhàn)指南
- 微信小程序自定義漸變的tabbar導(dǎo)航欄功能
- uniapp開發(fā)微信小程序自定義頂部導(dǎo)航欄功能實例
- 微信小程序?qū)崿F(xiàn)側(cè)邊導(dǎo)航欄
- uniapp微信小程序自定義導(dǎo)航欄的全過程
- 微信小程序自定義導(dǎo)航欄功能的實現(xiàn)
相關(guān)文章
js動態(tài)添加刪除,后臺取數(shù)據(jù)(示例代碼)
這篇文章主要是對js動態(tài)添加刪除,后臺取數(shù)據(jù)(示例代碼)進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11原生js基于canvas實現(xiàn)一個簡單的前端截圖工具代碼實例
這篇文章主要介紹了原生js基于canvas實現(xiàn)一個簡單的前端截圖工具代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09JS Jquery 遍歷,篩選頁面元素 自動完成(實現(xiàn)代碼)
本篇文章是對JS Jquery 遍歷,篩選頁面元素 自動完成的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友參考下2013-07-07