微信小程序自定义导航栏兼容适配所有机型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信小程序自定义导航栏兼容适配所有机型相关的知识,希望对你有一定的参考价值。
参考技术A <blockquote><p>目前小程序已在前端占了一席之地,最近公司项目上用的就是小程序开发,由于功能及页面不是很多,所以直接原生开发,毕竟坑可能会少点,在开发过程中,小程序自带导航栏和客户的设计稿导航栏排在一起,感觉很别扭,因此要求去掉微信的自带导航栏,微信提供了这方面的api,接下来我们就实操。</p></blockquote><div class="image-package"><img src=" https://upload-images.jianshu.io/upload_images/8877503-c85b39b2671d31b7.jpeg " img-data=""format":"jpeg","size":14897,"height":141,"width":963" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/></div><p><span style="font-size:15px">这是小程序官方文档截图,可以看到导航栏样式支持两种,默认是带导航栏,另外一种是自定义导航栏-custom,如果使用自定义导航栏,我们可以</span></p><p><strong><span style="font-size:15px">全局配置</span></strong><span style="font-size:15px"/></p><span style="font-size:inherit">//app.json</span>
<span style="font-size:inherit">"window"</span>:
<span style="font-size:inherit">"navigationStyle"</span>: <span style="font-size:inherit">"custom"</span>
<p><strong><span style="font-size:15px">单页面配置</span></strong></p><span style="font-size:inherit">//page.json</span>
<span style="font-size:inherit">"navigationStyle"</span>: <span style="font-size:inherit">"custom"</span>
<p><strong><span style="font-size:15px">效果对比</span></strong>
</p><div class="image-package"><img src=" https://upload-images.jianshu.io/upload_images/8877503-1d120c002c6ef37c.jpeg " img-data=""format":"jpeg","size":17446,"height":352,"width":922" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><span style="font-size:15px">能明显的看出来,自定义导航栏页面内容已经顶到屏幕顶端,除了胶囊按钮,其他都是页面可控区域。</span><span style="font-size:15px">每个手机的屏幕都不一样,各家系统的状态栏高度也不一样,因此,我们在开发页面时要考虑屏幕的适配,有刘海的,要留出刘海的距离,没有的,要把状态栏高度留出来。</span><p><strong><span style="font-size:16px">1.获取导航栏高度及按钮位置</span></strong>
</p><p><span style="font-size:15px">微信提供了获取导航栏高度的Api和胶囊按钮位置的Api</span>
</p><span style="font-size:inherit">// 系统信息</span>
<span style="font-size:inherit">const</span> systemInfo = wx.getSystemInfoSync();
<span style="font-size:inherit">// 胶囊按钮位置信息</span>
<span style="font-size:inherit">const</span> menuButtonInfo = wx.getMenuButtonBoundingClientRect();
<p><span style="font-size:15px">在控制台打印出这两个Api返回结果</span></p><div class="image-package"><img src=" https://upload-images.jianshu.io/upload_images/8877503-7925dd39fb0ec47a.jpeg " img-data=""format":"jpeg","size":39744,"height":497,"width":679" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p><span style="font-size:15px">这里面我们只说几个我们接下来用到的参数。</span></p>statusBarHeight <span style="font-size:inherit">// 状态栏高度</span>
screenWidth <span style="font-size:inherit">// 胶囊的宽度</span>
top <span style="font-size:inherit">// 胶囊到顶部距离</span>
height <span style="font-size:inherit">// 胶囊的高度</span>
right <span style="font-size:inherit">// 胶囊距离右边的距离</span>
<p><span style="font-size:15px">通过这几个参数,我们可以计算出状态栏的高度,微信胶囊所占的高度(存在padding值,可以使元素和胶囊纵向居中)</span>
</p><p><span style="font-size:15px">首先在app.js中定义全局data-globalData</span></p>globalData:
navBarHeight: <span style="font-size:inherit">0</span>, <span style="font-size:inherit">// 导航栏高度</span>
menuBotton: <span style="font-size:inherit">0</span>, <span style="font-size:inherit">// 胶囊距底部间距(保持底部间距一致)</span>
menuRight: <span style="font-size:inherit">0</span>, <span style="font-size:inherit">// 胶囊距右方间距(方保持左、右间距一致)</span>
menuHeight: <span style="font-size:inherit">0</span>, <span style="font-size:inherit">// 胶囊高度(自定义内容可与胶囊高度保证一致)</span>
,
<p><span style="font-size:15px">新建个方法</span>
</p>setNavBarInfo()
<span style="font-size:inherit">// 获取系统信息</span>
const systemInfo = wx.getSystemInfoSync();
<span style="font-size:inherit">// 胶囊按钮位置信息</span>
const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
<span style="font-size:inherit">// 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度</span>
<span style="font-size:inherit">this</span>.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * <span style="font-size:inherit">2</span> + menuButtonInfo.height + systemInfo.statusBarHeight;
<span style="font-size:inherit">this</span>.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
<span style="font-size:inherit">this</span>.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
<span style="font-size:inherit">this</span>.globalData.menuHeight = menuButtonInfo.right;
<p><span style="font-size:15px">在onLaunch中调用,因为我这个项目是所有的导航都不用微信自带的,所以在app.js</span>
</p><p><span style="font-size:15px">中调用及设置data。</span></p> onLaunch()
<span style="font-size:inherit">this</span>.setNavBarInfo()
,
<p><span style="font-size:15px"/><span style="font-size:15px">到这里所需要用到的都已经存了起来,页面用法也比较简单,排除状态栏的高度,设置导航栏的高度和胶囊高度保持,用flex布局。</span>
</p><p><strong><span style="font-size:16px">2.页面适配</span></strong></p><p><span style="font-size:15px">首先page.js中定义变量</span></p><span style="font-size:inherit">var</span> app = getApp()
Page(
<span style="font-size:inherit">/*
* 页面的初始数据
/</span>
data:
navBarHeight: app.globalData.navBarHeight, <span style="font-size:inherit">//导航栏高度</span>
menuBotton: app.globalData.menuBotton, <span style="font-size:inherit">//导航栏距离顶部距离</span>
menuRight: app.globalData.menuRight, <span style="font-size:inherit">//导航栏距离右侧距离</span>
menuHeight: app.globalData.menuHeight, <span style="font-size:inherit">//导航栏高度</span>
)
<p><span style="font-size:15px">页面使用</span>
</p><span style="font-size:inherit"><<span style="font-size:inherit">view</span> <span style="font-size:inherit">class</span>=<span style="font-size:inherit">"nav"</span> <span style="font-size:inherit">style</span>=<span style="font-size:inherit">"height:navBarHeightpx;"</span>></span>
<span style="font-size:inherit"><<span style="font-size:inherit">view</span> <span style="font-size:inherit">class</span>=<span style="font-size:inherit">"nav-main"</span>></span>
<span style="font-size:inherit"></span>
<span style="font-size:inherit"><<span style="font-size:inherit">view</span>
<span style="font-size:inherit">class</span>=<span style="font-size:inherit">"capsule-box"</span> <span style="font-size:inherit">style</span>=<span style="font-size:inherit">"style="</span><span style="font-size:inherit">height:</span>menuHeight+menuBotton 2<span style="font-size:inherit">px</span>; <span style="font-size:inherit">min-height:</span>menuHeight<span style="font-size:inherit">px</span>; <span style="font-size:inherit">line-height:</span>menuHeight<span style="font-size:inherit">px</span>; <span style="font-size:inherit">bottom:0px</span>;<span style="font-size:inherit">padding:0</span> menuRight<span style="font-size:inherit">px</span>;"></span>
<span style="font-size:inherit"></span>
<span style="font-size:inherit"><<span style="font-size:inherit">slot</span>></span><span style="font-size:inherit"></<span style="font-size:inherit">slot</span>></span>
<span style="font-size:inherit"></<span style="font-size:inherit">view</span>></span>
<span style="font-size:inherit"></<span style="font-size:inherit">view</span>></span>
<span style="font-size:inherit"></<span style="font-size:inherit">view</span>></span>
<p><span style="font-size:15px">wxss</span>
</p><span style="font-size:inherit">/ 公共导航 */</span>
<span style="font-size:inherit">.nav</span>
<span style="font-size:inherit">position</span>: fixed;
<span style="font-size:inherit">top</span>: <span style="font-size:inherit">0</span>;
<span style="font-size:inherit">left</span>: <span style="font-size:inherit">0</span>;
<span style="font-size:inherit">box-sizing</span>: border-box;
<span style="font-size:inherit">width</span>: <span style="font-size:inherit">100vw</span>;
<span style="font-size:inherit">z-index</span>: <span style="font-size:inherit">1000</span>;
<span style="font-size:inherit">.nav-main</span>
<span style="font-size:inherit">width</span>: <span style="font-size:inherit">100%</span>;
<span style="font-size:inherit">height</span>: <span style="font-size:inherit">100%</span>;
<span style="font-size:inherit">box-sizing</span>: border-box;
<span style="font-size:inherit">position</span>: relative;
<span style="font-size:inherit">.nav</span> <span style="font-size:inherit">.capsule-box</span>
<span style="font-size:inherit">position</span>: absolute;
<span style="font-size:inherit">box-sizing</span>: border-box;
<span style="font-size:inherit">width</span>: <span style="font-size:inherit">100%</span>;
<span style="font-size:inherit">display</span>: flex;
<span style="font-size:inherit">align-items</span>: center;
<p><span style="font-size:15px">最终效果</span></p><div class="image-package"><img src=" https://upload-images.jianshu.io/upload_images/8877503-ef017724a144e58c.jpeg " img-data=""format":"jpeg","size":4933,"height":105,"width":349" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p><span style="font-size:15px"/>
</p><p><span style="font-size:15px">此种适配方案适应所有手机,应该说是最优的选择。</span></p>
以上是关于微信小程序自定义导航栏兼容适配所有机型的主要内容,如果未能解决你的问题,请参考以下文章