微信小程序自定義導航的方法
本文實例為大家分享了微信小程序自定義導航的具體代碼,供大家參考,具體內(nèi)容如下
在app.js中獲取狀態(tài)欄信息和膠囊按鈕信息
onLaunch() {
? ? // 展示本地存儲能力
? ? const logs = wx.getStorageSync('logs') || []
? ? logs.unshift(Date.now())
? ? wx.setStorageSync('logs', logs)
? ? // 獲取系統(tǒng)信息
?? ?this.globalData.systemInfo = wx.getSystemInfoSync();
?? ?// 獲取狀態(tài)欄高度
?? ?this.globalData.statusBarHeight = this.globalData.systemInfo.statusBarHeight
?? ?// 膠囊按鈕位置信息
?? ?this.globalData.menuButtonInfo = wx.getMenuButtonBoundingClientRect();
?? ?// 獲取導航欄高度
?? ?this.globalData.navBarHeight = this.globalData.menuButtonInfo.bottom + (this.globalData.menuButtonInfo.top - this.globalData.statusBarHeight)
},globalData: {
? ? statusBarHeight: '',
? ? menuButtonInfo: {},
? ? navBarHeight:'',
? ? systemInfo:''
? },導航欄高度為膠囊底部位置+(膠囊頂部位置-狀態(tài)欄高度)
將導航欄封裝成組件

navigation-bar.js
properties: {
?? ?// 是否顯示返回箭頭
? ? showBackArrow: {
? ? ? ? type: Boolean,
? ? ? ? value: true
? ? },
? ? // 是否自定義導航欄標題
? ? customTitle: {
? ? ? ? type: Boolean,
? ? ? ? value: false
? ? },
? ? // 導航欄標題
? ? title: {
? ? ? ? type: String,
? ? ? ? value: 'weixin'
? ? },
? ? // 是否自定義返回方法
? ? customBack: {
? ? ? ? type: Boolean,
? ? ? ? value: false
? ? }
},
data: {
?? ?navBarHeight:getApp().globalData.navBarHeight,
? ? statusBarHeight:getApp().globalData.statusBarHeight,
? ? menuButtonInfo:getApp().globalData.menuButtonInfo
},
methods: {
? ? /** ?點擊返回按鈕 */
? ? back() {
? ? ? ? if (this.data.customBack) {
? ? ? ? ? ? this.triggerEvent('back')
? ? ? ? } else {
? ? ? ? ? ? wx.navigateBack({
? ? ? ? ? ? ? ? delta: 0,
? ? ? ? ? ? })
? ? ? ? }
? ? },
? ? /** 點擊導航欄標題事件 */
? ? clickTitle(){
? ? ? ? this.triggerEvent('clickTitle')
? ? },
}navigation-bar.wxml
<view class="nav-bar" style="height:{{navBarHeight}}px;">
? ? <view style="height:{{statusBarHeight}}px;"></view>
? ? <view style="height:{{navBarHeight-statusBarHeight}}px;width:{{menuButtonInfo.left}}px;" class="nav-box">
? ? ? ? <view class="back-arrow" wx:if="{{showBackArrow}}">
? ? ? ? ? ? <van-icon name="arrow-left" color="#262626" size="40rpx" bindtap="back"></van-icon>
? ? ? ? </view>
? ? ? ? <view class="nav-title" style="width: {{showBackArrow?'calc(100% - 40rpx)':'100%'}};">
? ? ? ? ? ? <text wx:if="{{!customTitle}}" bindtap="clickTitle">{{title}}</text>
? ? ? ? ? ? <slot wx:if="{{customTitle}}"></slot>
? ? ? ? </view>
? ? </view>
</view>
<view style="height:{{navBarHeight}}px;"></view>navigation-bar.wxss
.nav-bar{
? ? width: 100%;
? ? position: fixed;
? ? top: 0;
? ? left: 0;
? ? background-color: #ffffff;
? ? z-index: 1000000;
}
.nav-box{
? ? padding: 0 20rpx;
? ? display: flex;
? ? align-items: center;
}
.back-arrow{
? ? width: 60rpx;
? ? height: 100%;
? ? display: flex;
? ? align-items: center;
? ? padding-top: 4rpx;
}
.nav-title{
? ? height: 100%;
? ? display: flex;
? ? align-items: center;
? ? font-size: 36rpx;
? ? color: #262626;
? ? font-weight: 600;
}使用
app.js
"window": {
? ? ? ? "navigationStyle": "custom"
? ? },
? ? "usingComponents": {
? ? ? ? "navigation-bar":"/components/navigation-bar/navigation-bar",
? ? }.wxml
<navigation-bar title="打卡" customBack bind:back="backWorkPage"></navigation-bar>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Javascript四舍五入Math.round()與Math.pow()使用介紹
本文為大家介紹下Javascript中的四舍五入Math.round()與Math.pow()的使用,感興趣的朋友不要錯過2013-12-12
通過BootStrap-select插件 js jQuery控制select屬性變化
bootstrap-select我想大家都不陌生是一個前端下拉框的插件非常好用,在select的標簽中設置屬性可以做很多功能控制,下面通過本文給大家詳細介紹下2017-01-01
uniapp中使用render.js進行openlayers、arcgis等地圖操作實戰(zhàn)指南
renderjs是一個運行在視圖層的js,它比WXS更加強大,它只支持app-vue和h5,下面這篇文章主要給大家介紹了關于uniapp中使用render.js進行openlayers、arcgis等地圖操作的相關資料,需要的朋友可以參考下2024-01-01

