微信小程序自定义导航栏

如图所示: 实现自定义导航栏返回按钮,并和微信右侧"胶囊"按钮对齐的效果

一、使用getSystemInfo()获取设备状态栏高度,和使用getMenuButtonBoundingClientRect()获取右侧"胶囊信息"

data() {

       return {

       statusBarh:'', //状态栏高度

       customViewHeight:'', //自定义返回按钮高度

      };

},

wx.getSystemInfo({

     success:res => {

    console.log('设备信息',res)

    this.statusBarh = res.statusBarHeight

}

})

let btn = wx.getMenuButtonBoundingClientRect()


//getSystemInfo获取状态栏信息如下

//getMenuButtonBoundingClientRect()获取"胶囊"按钮信息如下

二、计算自定义返回按钮的高度分析

注意:胶囊顶部btn.top包含了statusBarHeight, 所以 (btn.top-this.statusBarh) 就是胶囊按钮顶部距离状态栏的间距

2.1、整个导航栏的高度 = this.statusBarh + 2 * (btn.top-this.statusBarh) + btn.height

2.2 设置自定义返回按钮顶部top为statusBarh ,固可设置其高度为

this.customViewHeight = 2 * (btn.top-this.statusBarh) + btn.height

三、布局代码如下

微信开发者工具布局

<view class="flex align-center justify-center" @click="navigateBack" style="position: fixed;top: {{statusBarh}}px;height: {{customViewHeight}}px;width: {{statusBarh}}px;background-color: #0081FF;">

    <text class="cuIcon-back text-white"></text>

</view>

HBuilderX、Vue布局

<view class="flex align-center justify-center" @click="navigateBack"

:style="'position: fixed;top: '+ statusBarh + 'px;height: ' + customViewHeight +'px;width: ' + statusBarh + 'px;background-color: ' + '#0081FF;'">

    <text class="cuIcon-back text-white"></text>

</view>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

相关阅读更多精彩内容

友情链接更多精彩内容