GVKun编程网logo

微信小程序自定义导航栏兼容适配所有机型(小程序自定义导航栏适配(完美版))

23

对于微信小程序自定义导航栏兼容适配所有机型感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解小程序自定义导航栏适配(完美版),并且为您提供关于uniapp中小程序自定义导航栏、uniapp开发

对于微信小程序自定义导航栏兼容适配所有机型感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解小程序自定义导航栏适配(完美版),并且为您提供关于uniapp中小程序自定义导航栏、uniapp开发微信小程序自定义顶部导航栏功能实例、uniapp微信小程序自定义导航栏的全过程、【小程序】微信小程序自定义导航栏及其封装的宝贵知识。

本文目录一览:

微信小程序自定义导航栏兼容适配所有机型(小程序自定义导航栏适配(完美版))

微信小程序自定义导航栏兼容适配所有机型(小程序自定义导航栏适配(完美版))

目前小程序已在前端占了一席之地,最近公司项目上用的就是小程序开发,由于功能及页面不是很多,所以直接原生开发,毕竟坑可能会少点,在开发过程中,小程序自带导航栏和客户的设计稿导航栏排在一起,感觉很别扭,因此要求去掉微信的自带导航栏,微信提供了这方面的api,接下来我们就实操。

这是小程序官方文档截图,可以看到导航栏样式支持两种,默认是带导航栏,另外一种是自定义导航栏-custom,如果使用自定义导航栏,我们可以

全局配置

//app.json
window: {
 navigationStyle: custom
}

单页面配置

//page.json
{
 navigationStyle: custom
}

效果对比

能明显的看出来,自定义导航栏页面内容已经顶到屏幕顶端,除了胶囊按钮,其他都是页面可控区域。每个手机的屏幕都不一样,各家系统的状态栏高度也不一样,因此,我们在开发页面时要考虑屏幕的适配,有刘海的,要留出刘海的距离,没有的,要把状态栏高度留出来。

1.获取导航栏高度及按钮位置

微信提供了获取导航栏高度的Api和胶囊按钮位置的Api

// 系统信息
const systemInfo = wx.getSystemInfoSync();
// 胶囊按钮位置信息
const menuButtonInfo = wx.getMenuButtonBoundingClientRect();

在控制台打印出这两个Api返回结果

这里面我们只说几个我们接下来用到的参数。

statusBarHeight     // 状态栏高度
screenWidth         // 胶囊的宽度

top                 // 胶囊到顶部距离
height              // 胶囊的高度
right               // 胶囊距离右边的距离

通过这几个参数,我们可以计算出状态栏的高度,微信胶囊所占的高度(存在padding值,可以使元素和胶囊纵向居中)

首先在app.js中定义全局data-globalData

globalData: {
    navBarHeight: 0,     // 导航栏高度
    menuBotton: 0,       // 胶囊距底部间距(保持底部间距一致)
    menuRight: 0,        // 胶囊距右方间距(方保持左、右间距一致)
    menuHeight: 0,       // 胶囊高度(自定义内容可与胶囊高度保证一致)
},

新建个方法

setNavBarInfo() {
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync();
    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度
    this.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight;
    this.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
    this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    this.globalData.menuHeight = menuButtonInfo.right;
 }

在onLaunch中调用,因为我这个项目是所有的导航都不用微信自带的,所以在app.js

中调用及设置data。

 onLaunch() {
    this.setNavBarInfo()
 },

到这里所需要用到的都已经存了起来,页面用法也比较简单,排除状态栏的高度,设置导航栏的高度和胶囊高度保持,用flex布局。

2.页面适配

首先page.js中定义变量

var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight, //导航栏高度
    menuBotton: app.globalData.menuBotton, //导航栏距离顶部距离
    menuRight: app.globalData.menuRight, //导航栏距离右侧距离
    menuHeight: app.globalData.menuHeight, //导航栏高度
  }
})

页面使用

<view class=nav style=height:{{navBarHeight}}px;>
 <view class=nav-main>
 <!-- 胶囊区域 -->
 <view 
  class=capsule-Box  style=style=height:{{menuHeight+menuBotton*2}}px; min-height:{{menuHeight}}px; line-height:{{menuHeight}}px; bottom:0px;padding:0 {{menuRight}}px;>
 <!-- 导航内容区域 -->
  <slot></slot>
 </view>
 </view>
</view>

wxss

/* 公共导航 */
.nav {
  position: fixed;
  top: 0;
  left: 0;
  Box-sizing: border-Box;
  width: 100vw;
  z-index: 1000;
 }
 .nav-main {
  width: 100%;
  height: 100%;
  Box-sizing: border-Box;
  position: relative;
 }
 .nav .capsule-Box {
  position: absolute;
  Box-sizing: border-Box;
  width: 100%;
  display: flex;
  align-items: center;
 }

最终效果

此种适配方案适应所有手机,应该说是最优的选择。

uniapp中小程序自定义导航栏

uniapp中小程序自定义导航栏

1、如果需要使用自定义导航栏的时候,需要在page.json文件中做如下更改

"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
        {
            "path": "pages/list/index",
            "style": {
                "navigationBarTitleText": "list",
                "navigationStyle":"custom"//添加自定义配置
            }
        },
        {
            "path": "pages/index/index",
            "style": {
                "navigationBarTitleText": "home"
            }
        }
    ],

2、配置完成之后,自定义导航有如下写法

1)固定的状态栏高度,此时iphonex等手机不建议使用

<template>
    <view>
        <view>
            <!-- 这里是状态栏 -->
        </view>
        <view> 状态栏下的文字 </view>
    </view>
</template>    
<style>
    .status_bar {
        height: var(--status-bar-height);
        width: 100%;
        background: red;
    }
</style>

2)自定义写法,根据对应机型自行调整,所有机型都可使用

<template>
    <view>
        <!-- 假设我需要状态栏到文字内容部分还有50px的距离 -->
        <view:>
            <text>list</text>
        </view>
        <view> 状态栏下的文字 </view>
    </view>
</template>  

<script>
    export default{
        data(){
            return {
                height:null,//获取的状态栏高度
            }
        },
        onLoad(){
            var _this=this;
            // 获取手机状态栏高度
            uni.getSystemInfo({
                success:function(data){
                    // 将其赋值给this
                    _this.height=data.statusBarHeight;
                }
            })
        },
    }
</script>

<style>
    .status_bar {
        width: 100%;
        background: #007AFF;
        position: relative;
    }
    /* 调整状态栏标题的位置 */
    text{
        position: absolute;
        margin: auto;
        bottom:10px;
        left:0;
        right:0;
        text-align: center;
    }
</style>

 

uniapp开发微信小程序自定义顶部导航栏功能实例

uniapp开发微信小程序自定义顶部导航栏功能实例

自定义导航栏渐变色,先上效果

使用uniapp开发小程序,在不同界面,要去对页面进行修改顶部导航栏。

比如说要去定义导航栏的背景颜色,常规的去定义导航栏背景颜色

全局定义导航栏

"window": {
   "navigationBarBackgroundColor": "#32A2FD",  // 顶部背景颜色
    "navigationBarTitleText": "123456",         // 顶部文字
    "navigationStyle": "default",               // 是否自定义导航栏,当"default"为"custom"时开启自定义头部导航栏选项
    "navigationBarTextStyle": "white",          // 顶部文字颜色 仅支持 white/black    
},

单页面定义导航栏

"path": "pages/cargo/pickUpGoods",//页面路径
"style": {
	"navigationBarTitleText": "uni-app", // 顶部文字
	"navigationBarBackgroundColor": "#fff", // 顶部背景颜色
	"navigationBarTextStyle": "black" // 顶部文字颜色
 
}

重点来了,导航栏设置渐变色

踩坑,开始我以为把顶部导航栏的颜色换成渐变的就可以了,但是不行

查了之后才知道,设置渐变色要去自定义背景颜色

首先  如果是全部页面就在window里面添加,如果是单页面就在页面添加

"navigationStyle": "custom"

"path": "pages/cargo/shipments",
"style": {
	"navigationBarTitleText": "uni-app",
	"navigationStyle": "custom",//设置自定义导航栏
}

然后,自己封装一个组件,

<template>
	<view>
		<view:>
			<!-- 左侧返回按钮 -->
			<view@click="onBack" v-if="back" :>
				<uni-icons type="arrowleft" size="30" :color=''color''></uni-icons>
				<!-- 此处图标使用的是 uni-ui图标 -->
			</view>
			<!-- 中间标题文字 -->
			<view>
				{{title}}
			</view>
		</view>
	</view>
</template>
 
<script>
	export default {
		data() {
			return {
				height: 0, 
				paddingTop: 0,
				
			}
		},
		// props: ["title", "back"],
		props:{
			title:{ // 标题文字(默认为空)
				type:String,
				default:''''
			},
			color:{ // 标题和返回按钮颜色(默认白色)
				type:String,
				default:''#fff''
			},
            //建议使用background  因为使用backgroundColor,会导致不识别渐变颜色
			background:{ // 背景颜色(不传值默认透明)
				type:String,
				default:''transparent''
			},
			back:{ // 是否显示返回按钮(不传值默认不显示)
				type:Boolean,
				default:false
			},
		},
		
		created() {
			const demo = uni.getMenuButtonBoundingClientRect()
			this.height = demo.height + "px"
			this.paddingTop = demo.top + "px"
 
		},
		methods: {
			// 左侧返回按钮调用
			onBack() {
				this.$emit("onBack")
			}
		}
	}
</script>
<style lang="less">
	.demo {
		position: relative;//注意,建议使用相对定位,因为固定定位会脱离文档流,然后你还要去设置marginTop值
		// position: fixed;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		z-index: 100;
		padding-bottom: 10rpx;
 
		.left {
			float: left;
			position: absolute;
			width: 100rpx;
			height: 50rpx;
			top: 0;
			bottom: 0;
			left: 20rpx;
			color: #fff;
			margin: auto;
		}
 
		.title {
			font-size: 36rpx;
			font-family: Source Han Sans CN;
			// color: #FFFFFF;
		}
	}
</style>

然后,引入你的这个组件,写在页面的最上面

 代码在这里

<navbar:background="backgroundColor" back :title="title" @onBack="goBack"></navbar>

引入组件,使用

补充:更换图标

1.在阿里巴巴矢量图选择自己喜欢的图标,然后点击收藏

2.右上角下载全部已经收藏了的图标

3.在编辑器打开已经下载的文件,把文件里的iconfont.ttf丢到static文件夹里,然后再打开iconfont.css里查看unicode编码

4.最后把对应图标的编码填写到page.json的配置项里text,需要写成一个"\u***",然后重启就实现了

5.最后在对应的页面生命周期方法里填写,通过e.index,来配置不同的方法

        onNavigationBarButtonTap:function(e){
            console.log(JSON.stringify(e))
        },

踩了很多坑,制作不易。

总结

到此这篇关于uniapp开发微信小程序自定义顶部导航栏的文章就介绍到这了,更多相关uniapp自定义顶部导航栏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能感兴趣的文章:
  • uniapp小程序配置tabbar底部导航栏实战指南
  • 微信小程序自定义渐变的tabbar导航栏功能
  • 微信小程序实现左侧导航栏
  • 微信小程序实现简单搜索框
  • 微信小程序实现搜索框功能
  • 微信小程序使用uni-app实现首页搜索框导航栏功能详解

uniapp微信小程序自定义导航栏的全过程

uniapp微信小程序自定义导航栏的全过程

前言

相信很多小伙伴在使用uniapp进行多端开发的时候呢,在面对一些奇葩的业务需求的时候,uniapp给我们提供的默认导航栏已经不能满足我们的业务需求了,这个时候就需要我们自己自定义导航栏使用啦。

当然uniapp也给我们提供了很多的自定义导航栏的插件供大家使用,今天也给大家分享一个我自己写的导航栏啦,希望大家多多指点

首先我们在自定义导航栏的时候,我们需要知道头部的导航栏有哪几部分组成,那么我们以微信小程序为例

可以看到在微信小程序中,我们的头部导航栏其实受到右上角胶囊的限制比较大,这个时候我们自定义的导航栏,需要做到标题于胶囊水平对齐,那其实这个时候整个头部其实主要又:状态栏的高度+标题栏的高度组成。

状态栏的高度我们可以通过uni.getSystemInfoSync().statusBarHeight来获取。

那么标题栏的高度我们怎么获取呢?

其实要想定义标题栏的高度,我们需要知道这个胶囊的位置,在小程序中我们可以使用wx.getMenuButtonBoundingClientRect()获取关于胶囊的信息

获取到的胶囊的top,left,right分别对应胶囊的上边界,左边界,右边界相对于屏幕左上角的起点的位置,所以我们是不是可以用(胶囊上边界距离顶部的距离 - 状态栏的高度)*2+胶囊的高度,就是标题栏的高度呢?然后再在标题栏里面添加一个文本区让他的高度等于胶囊的高度,实现flex布局的上下居中是不是就搞定了呢?

以上呢其实针对小程序平台的导航栏讲解,那么既然使用uniapp,就会考虑到多端情况

那么其实我们使用uniapp获取到的状态栏在h5,小程序和app原生都是有效的,h5网页中一般我们都是直接采用浏览器内核给我们内置的网页导航栏,就是一个头部,没有过多的要求,而且浏览器不同,给我们提供的头部导航也不一样。

而对于app端,我们没有了像微信小程序中那种让人心烦的胶囊之后,我们只需要知道状态栏的高度,然后加上自己业务需求的标题栏样式和标题栏高度就行啦

所以我们在进行自定义导航栏封装的时候就要对代码进行条件编译啦。那么我这里呢是把微信小程序做了单独的处理,微信小程序之外的平台看作是统一状态

献上源码:

首先我们把获取设备信息的代码封装到一个统一的js文件里面,这样方便我们在组件中获取也方便我们在页面中获取。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * 此js文件管理关于当前设备的机型系统信息
 */
const systemInfo = function() {
    /****************** 所有平台共有的系统信息 ********************/
    // 设备系统信息
    let systemInfomations = uni.getSystemInfoSync()
    // 机型适配比例系数
    let scaleFactor = 750 / systemInfomations.windowWidth
    // 当前机型-屏幕高度
    let windowHeight = systemInfomations.windowHeight * scaleFactor //rpx
    // 当前机型-屏幕宽度
    let windowWidth = systemInfomations.windowWidth * scaleFactor //rpx
    // 状态栏高度
    let statusBarHeight = (systemInfomations.statusBarHeight) * scaleFactor //rpx
  
    // 导航栏高度  注意:此导航栏高度只针对微信小程序有效 其他平台如自定义导航栏请使用:状态栏高度+自定义文本高度
    let navHeight = 0 //rpx
    // console.log(windowHeight,''哈哈哈哈哈'');
     
    /****************** 微信小程序头部胶囊信息 ********************/
    // #ifdef MP-WEIXIN
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
    // 胶囊高度
    let menuButtonHeight = menuButtonInfo.height * scaleFactor //rpx
    // 胶囊宽度
    let menuButtonWidth = menuButtonInfo.width * scaleFactor //rpx
    // 胶囊上边界的坐标
    let menuButtonTop = menuButtonInfo.top * scaleFactor //rpx
    // 胶囊右边界的坐标
    let menuButtonRight = menuButtonInfo.right * scaleFactor //rpx
    // 胶囊下边界的坐标
    let menuButtonBottom = menuButtonInfo.bottom * scaleFactor //rpx
    // 胶囊左边界的坐标
    let menuButtonLeft = menuButtonInfo.left * scaleFactor //rpx
  
    // 微信小程序中导航栏高度 = 胶囊高度 + (顶部距离 - 状态栏高度) * 2
    navHeight = menuButtonHeight + (menuButtonTop - statusBarHeight) * 2
    // #endif
  
  
    // #ifdef MP-WEIXIN
    return {
        scaleFactor,
        windowHeight,
        windowWidth,
        statusBarHeight,
        menuButtonHeight,
        menuButtonWidth,
        menuButtonTop,
        menuButtonRight,
        menuButtonBottom,
        menuButtonLeft,
        navHeight
    }
    // #endif
  
    // #ifndef MP-WEIXIN
    return {
        scaleFactor,
        windowHeight,
        windowWidth,
        statusBarHeight
    }
    // #endif
}
  
export {
    systemInfo
}

然后我们定义一个导航栏组件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* 注意:
* 1、在传入宽度或者高度时,如果是Number数据,传入的值为px大小,无需带单位,组件自动计算
* 2、在使用此导航栏时,建议传入UI规定的导航栏高度,此高度只针对除微信小程序的其他平台有效,微信小程序的导航栏高度,组件自计算
*/
<template>
    <view>
        <!-- 微信小程序头部导航栏 -->
        <!-- #ifdef MP-WEIXIN -->
        <view class="wx-head-mod" :style="{height:navHeight+''rpx'',backgroundColor:navBackgroundColor}">
            <view class="wx-head-mod-nav" :style="{height:navigationBarHeight+''rpx'',top:statusBarHeight+''rpx''}">
                <view class="wx-head-mod-nav-content"
                    :style="{height:customHeight+''rpx'',justifyContent:textAlign === ''center''?''center'':''left''}">
                    <!-- 文本区 -->
                    <view class="wx-head-mod-nav-content-mian"
                        :style="{width:navTextWidth,lineHeight:customHeight + ''rpx'',paddingLeft:textPaddingLeft*scaleFactor+''rpx'',fontSize:fontSize*scaleFactor+''rpx'',fontWeight:fontWeight,color:titleColor}">
                        {{textContent}}
                    </view>
                    <!-- 返回按钮 -->
                    <view class="wx-head-mod-nav-content-back" :style="{display:isBackShow?''flex'':''none''}"
                        @click="backEvent">
                        <view class="wx-head-mod-nav-content-back-img"
                            :style="{width:backImageWidth*scaleFactor+''rpx'',height:backImageHeight*scaleFactor+''rpx''}">
                            <image :src="backImageUrl" mode="" style="width: 100%;height: 100%;"></image>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <!-- #endif -->
  
        <!-- 除微信小程序之外的其他设备 -->
        <!-- #ifndef MP-WEIXIN -->
        <view class="other-head-mod"
            :style="{height:navHeightValue*scaleFactor+statusBarHeight+''rpx'',backgroundColor:navBackgroundColor}">
            <view class="other-head-mod-mian"
                :style="{height:navHeightValue*scaleFactor+''rpx'',justifyContent:textAlign === ''center''?''center'':''left''}">
                <!-- 返回按钮 -->
                <view class="other-head-mod-mian-back" v-show="isBackShow" @click="backEvent">
                    <view class="other-head-mod-mian-back-img"
                        :style="{width:backImageWidth*scaleFactor+''rpx'',height:backImageHeight*scaleFactor+''rpx''}">
                        <image :src="backImageUrl" mode="" style="width: 100%;height: 100%;"></image>
                    </view>
                </view>
                <!-- 标题 -->
                <view class="other-head-mod-mian-title" :style="{width:windowWidth - 184+''rpx'',lineHeight:navHeightValue*scaleFactor+''rpx'',
                    paddingLeft:textPaddingLeft*scaleFactor+''rpx'',fontSize:fontSize*scaleFactor+''rpx'',
                    fontWeight:fontWeight,color:titleColor}">
                    {{textContent}}
                </view>
            </view>
        </view>
        <!-- #endif -->
    </view>
</template>
  
<script>
    const app = getApp()
    import {systemInfo} from ''@/common/system-info.js''
    export default {
        name: "HeadView",
        props: {
            // 文本区域位置 left:左  center:中 
            textAlign: {
                type: String,
                default: ''center''
            },
            // 文本区内容
            textContent: {
                type: String,
                default: ''哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈就啊哈哈好借好还''
            },
            // 文本区离左边的距离
            textPaddingLeft: {
                type: Number,
                default: 16
            },
            // 是否需要返回按钮
            isBackShow: {
                type: Boolean,
                default: true
            },
            // 文本区字体大小
            fontSize: {
                type: Number,
                default: 20 //px
            },
            // 文本区字体粗细
            fontWeight: {
                type: Number,
                default: 700
            },
            // 文本区返回按钮图片宽
            backImageWidth: {
                type: Number,
                default: 12 //px
            },
            // 文本区返回按钮图片高
            backImageHeight: {
                type: Number,
                default: 24 //px
            },
            // 返回按钮图标路径
            backImageUrl: {
                type: String,
                default: ''/static/backImage.svg''
            },
            // 导航栏整体背景颜色
            navBackgroundColor: {
                type: String,
                default: ''#2476F9''
            },
            // 标题字体颜色
            titleColor: {
                type: String,
                default: ''#ffffff'',
            },
  
            /******** h5端,app端需要传入自定义导航栏高度 *******/
            navHeightValue: {
                type: Number,
                default: 44 //px
            }
        },
        computed: {
            // 文本区宽度
            navTextWidth() {
                if (this.textAlign === ''center'') {
                    return (this.windowWidth - (this.windowWidth - this.menubarLeft) * 2) + ''rpx''
                } else {
                    return this.menubarLeft + ''rpx''
                }
            },
            // 文本区paddingLeft
            textPaddingleft() {
                if (this.textAlign === ''center'') {
                    return ''0''
                } else {
                    return this.textPaddingLeft + ''rpx''
                }
            }
        },
        data() {
            return {
                statusBarHeight: app.globalData.statusBarHeight, //状态栏高度
                navHeight: app.globalData.navHeight, //头部导航栏总体高度
                navigationBarHeight: app.globalData.navigationBarHeight, //导航栏高度
                customHeight: app.globalData.customHeight, //胶囊高度
                scaleFactor: app.globalData.scaleFactor, //比例系数
                menubarLeft: app.globalData.menubarLeft, //胶囊定位的左边left
                windowWidth: app.globalData.windowWidth * app.globalData.scaleFactor
            };
        },
        methods: {
            backEvent() {
                uni.navigateBack({
                    delta: 1
                })
            }
        },
        created() {
            /* 获取设备信息 */
            const SystemInfomations = systemInfo()
            /* 通用平台 */
            this.statusBarHeight = SystemInfomations.statusBarHeight //状态栏高度
            this.scaleFactor = SystemInfomations.scaleFactor //比例系数
            this.windowWidth = SystemInfomations.windowWidth //当前设备的屏幕宽度
            /* 微信小程序平台 */
            // #ifdef MP-WEIXIN
            this.navHeight = SystemInfomations.navHeight + SystemInfomations.statusBarHeight //头部导航栏总高度
            this.navigationBarHeight = SystemInfomations.navHeight //头部导航栏高度
            this.customHeight = SystemInfomations.menuButtonHeight //胶囊高度
            this.menubarLeft = SystemInfomations.menuButtonLeft //胶囊左边界距离左上角的距离
            // #endif
             
        }
    }
</script>
  
<style>
    /* #ifdef MP-WEIXIN */
    .wx-head-mod {
        box-sizing: border-box;
        width: 100%;
        position: fixed;
        top: 0;
        left: 0;
    }
  
    .wx-head-mod-nav {
        box-sizing: border-box;
        width: 100%;
        position: absolute;
        left: 0;
        display: flex;
        justify-content: center;
        align-items: center;
  
    }
  
    .wx-head-mod-nav-content {
        box-sizing: border-box;
        width: 100%;
        display: flex;
        justify-content: left;
        align-items: center;
        position: relative;
    }
  
    /* 文本区 */
    .wx-head-mod-nav-content-mian {
        box-sizing: border-box;
        height: 100%;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
    }
  
    /* 返回按钮 */
    .wx-head-mod-nav-content-back {
        box-sizing: border-box;
        width: 60rpx;
        height: 100%;
        /* background-color: aqua; */
        position: absolute;
        top: 0;
        left: 32rpx;
        display: flex;
        align-items: center;
        justify-content: left;
    }
  
    .wx-head-mod-nav-content-back-img {
        box-sizing: border-box;
    }
  
    /* #endif */
  
    /* #ifndef MP-WEIXIN */
    .other-head-mod {
        box-sizing: border-box;
        width: 100%;
        position: fixed;
        top: 0;
        left: 0;
    }
  
    .other-head-mod-mian {
        box-sizing: border-box;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: left;
        position: absolute;
        left: 0;
        bottom: 0;
    }
  
    /* 返回按钮 */
    .other-head-mod-mian-back {
        box-sizing: border-box;
        height: 100%;
        width: 60rpx;
        position: absolute;
        left: 32rpx;
        top: 0;
        display: flex;
        align-items: center;
    }
  
    /* 标题 */
    .other-head-mod-mian-title {
        box-sizing: border-box;
        height: 100%;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
    }
  
    /* #endif */
</style>

组件使用:

引入组件:

?
1
import HeadNav from ''@/components/HeadNav.vue''

组册组件:

?
1
2
3
components:{
            HeadNav
        },
?
1
2
3
4
5
6
<template>
    <view class="content">
        <head-nav></head-nav>
        <view class="content-main"></view>
    </view>
</template>

效果图:

微信小程序:

h5:

app:

 在项目里面没有针对h5时候需要导航栏做特别的限制,如果实际开发中在h5端不需要此导航栏,请在模版上面针对h5页面加入条件编译即可。

总结

到此这篇关于uniapp微信小程序自定义导航栏的文章就介绍到这了,更多相关uniapp自定义导航栏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能感兴趣的文章:

【小程序】微信小程序自定义导航栏及其封装

【小程序】微信小程序自定义导航栏及其封装

今天关于微信小程序自定义导航栏兼容适配所有机型小程序自定义导航栏适配(完美版)的分享就到这里,希望大家有所收获,若想了解更多关于uniapp中小程序自定义导航栏、uniapp开发微信小程序自定义顶部导航栏功能实例、uniapp微信小程序自定义导航栏的全过程、【小程序】微信小程序自定义导航栏及其封装等相关知识,可以在本站进行查询。

本文标签:

上一篇微信小程序云开发跨账号环境(资源)共享(微信小程序跨平台)

下一篇被微信面麻了,问的太细节了