|
+ S g& o: v- r" ]3 W2 S 目前小程序已在前端占了一席之地,最近公司项目上用的就是小程序开发,由于功能及页面不是很多,所以直接原生开发,毕竟坑可能会少点,在开发过程中,小程序自带导航栏和客户的设计稿导航栏排在一起,感觉很别扭,因此要求去掉微信的自带导航栏,微信提供了这方面的api,接下来我们就实操。
- e4 A$ |; y9 k: A: O7 b N7 U 这是小程序官方文档截图,可以看到导航栏样式支持两种,默认是带导航栏,另外一种是自定义导航栏-custom,如果使用自定义导航栏,我们可以全局配置//app.json"window": {"navigationStyle" . @: w* [+ g: N9 C$ `" J0 M
: "custom"}单页面配置//page.json{"navigationStyle": "custom"}效果对比 7 y: w# Z% X# f. T# x
能明显的看出来,自定义导航栏页面内容已经顶到屏幕顶端,除了胶囊按钮,其他都是页面可控区域每个手机的屏幕都不一样,各家系统的状态栏高度也不一样,因此,我们在开发页面时要考虑屏幕的适配,有刘海的,要留出刘海的距离,没有的,要把状态栏高度留出来。
7 l- M+ o1 N, A; s 1.获取导航栏高度及按钮位置微信提供了获取导航栏高度的Api和胶囊按钮位置的Api// 系统信息const systemInfo = wx.getSystemInfoSync();// 胶囊按钮位置信息
+ @6 c& {) Z+ b8 Y/ V, z) Y% }) k* V: r const menuButtonInfo = wx.getMenuButtonBoundingClientRect();在控制台打印出这两个Api返回结果
- J! r# l, F' t! B' x' k 这里面我们只说几个我们接下来用到的参数statusBarHeight // 状态栏高度screenWidth // 胶囊的宽度top // 胶囊到顶部距离height // 胶囊的高度right // 胶囊距离右边的距离。
0 }1 B) |5 G( P5 d) t9 \& Q6 A- t 通过这几个参数,我们可以计算出状态栏的高度,微信胶囊所占的高度(存在padding值,可以使元素和胶囊纵向居中)首先在app.js中定义全局data-globalDataglobalData: { navBarHeight: ; L, d0 b: \" ]6 s1 [" D# u
0, // 导航栏高度 menuBotton: 0, // 胶囊距底部间距(保持底部间距一致) menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致) menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
" @8 b8 i. v1 M },新建个方法setNavBarInfo() {// 获取系统信息 const systemInfo = wx.getSystemInfoSync();// 胶囊按钮位置信息 const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
$ H0 \0 P4 T! {) I9 l // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度this.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) *
, K( z* F5 z8 Y. G& c- B: v$ `3 t 2 + menuButtonInfo.height + systemInfo.statusBarHeight;this.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight; 5 t5 M7 p, N) F5 p( W
this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;this.globalData.menuHeight = menuButtonInfo.right; . o% ]( ?7 E4 K+ h$ M& l: v
}在onLaunch中调用,因为我这个项目是所有的导航都不用微信自带的,所以在app.js中调用及设置data onLaunch() {this.setNavBarInfo() },到这里所需要用到的都已经存了起来,页面用法也比较简单,排除状态栏的高度,设置导航栏的高度和胶囊高度保持,用flex布局。
2 J6 h1 x9 Y- H3 C# e ^ 2.页面适配首先page.js中定义变量var app = getApp()Page({/** * 页面的初始数据 */ data: { navBarHeight: app.globalData.navBarHeight, , a! ]- }& K1 M/ e7 ~+ \
//导航栏高度 menuBotton: app.globalData.menuBotton, //导航栏距离顶部距离 menuRight: app.globalData.menuRight, //导航栏距离右侧距离
' N0 x0 P w: R3 k! I menuHeight: app.globalData.menuHeight, //导航栏高度 }})页面使用
+ j: P' p/ ^$ J > 3 F) m7 I5 e! e+ w
px; min-height:{{menuHeight}}px; line-height:{{menuHeight}}px; bottom:0px;padding:0 {{menuRight}}px;"> p$ y6 v6 u# y0 v: O1 ~6 Z
wxss/* 公共导航 */.nav {position: fixed;top: 0;left: 0;box-sizing
0 ?4 n& y6 w; H- K- W. X : border-box;width: 100vw;z-index: 1000; }.nav-main {width: 100%;height: 100%;box-sizing: border-box; : s8 q0 O2 y( n
position: relative; }.nav.capsule-box {position: absolute;box-sizing: border-box;width: 100%;display: flex;
4 H! b1 k# }) }6 \ align-items: center; }最终效果
$ O+ ?( } ^0 D# s% { 此种适配方案适应所有手机,应该说是最优的选择。
0 Q; X% [% G2 t; Y* w( {
& ^( e) m9 ]. E) _3 J1 s
. X3 t, T+ y; m, x% o
2 V8 G. w& \& b) f# X
0 V) b) ^' W- j0 [9 E |