微信小程序如何实现自定义导航栏、导航栏手机适配(获取导航栏、状态栏高度和胶囊位置)以及踩过的坑

背景:不用官方默认的导航栏,想用自己自定义的

实现效果:

关键词:顶部状态栏、顶部导航栏、顶部状态导航栏、胶囊

原理:

自定义导航栏无非就是求得导航栏高度,并让内容容器垂直方向居中于导航栏高度

1.获取手机系统状态栏高度

2.获取胶囊位置(包括高度)

3.求得导航栏高度(导航栏高度实际上就是胶囊高度+高度差*2)

高度差 = 胶囊位置到屏幕顶部距离(menu.top,menu为胶囊位置对象)-状态栏高度(直接获取res.statusBarHeight)

(所有单位长度默认px)


     // 获取手机系统信息
     wx.getSystemInfo({
      success: res => {
     
        // 手机系统状态栏高度
        wx.setStorageSync('statusBarHeight', res.statusBarHeight)

        const platform = res.platform
        const menu =  wx.getMenuButtonBoundingClientRect()

        //menu为胶囊,判断是否能读到胶囊位置,读不到则用具体一般数值表示
        if (menu) {
          wx.setStorageSync('menu', menu)

          // 导航栏高度
          wx.setStorageSync('navBarHeight', menu.height+(menu.top-res.statusBarHeight) *2 )

          // 状态栏加导航栏

          wx.setStorageSync('navStatusBarHeight', res.statusBarHeight+ menu.height+(menu.top-res.statusBarHeight) *2 )


        }else{
          wx.setStorageSync('menu', null)

          // 导航栏高度
          wx.setStorageSync('navBarHeight', platform === 'android' ? 48 : 44)

           // 状态栏加导航栏

           wx.setStorageSync('navStatusBarHeight', res.statusBarHeight+ (platform === 'android' ? 48 : 44) )

        }

      }, fail(err) {
        console.log(err);
      }
    })

那么如何保持胶囊位置左边的搜索图标呢?

获取到胶囊到屏幕左侧的值,意思是menu.left,让容器等于这个menu.left值,然后让图标水平靠右即可

示例代码:

wxml

<view class="navigation-container" style="{{'height: ' + navStatusBarHeight +'px'}}">
  <!--空白来占位状态栏-->
  <view style="{{'height: ' + statusBarHeight +'px'}}"></view>
  <!--自定义导航栏-->
  <view class="navigation-bar" style="{{'height:' + navBarHeight +'px'}}">
    <view class="nav-title" style="height: 100%;">
      <view class="bg-logo">
        <text style="letter-spacing: 2rpx;font-weight: bold;font-size: 18px;">翡翠世界</text>
        <text style="font-size: 26rpx;letter-spacing: 2rpx;font-size: 10px;">交易走淘宝 | 48H鉴赏期</text>
      </view>
    </view>
    <view class="nav-search" style="height: 100%;width: {{menu.left ? menu.left : 281}}px;">
      <image  bindtap="search" src="../../images/icon/search.png" style="width:25px;height: 25px;padding-right: 10px;" />
    </view>
  </view>
</view>

js


  data: {

    // 状态栏高度
    statusBarHeight: wx.getStorageSync('statusBarHeight'),
    // 导航栏高度
    navBarHeight: wx.getStorageSync('navBarHeight'),

    // 导航栏和状态栏高度
    navStatusBarHeight: wx.getStorageSync('navStatusBarHeight'),

    // 胶囊
    menu:wx.getStorageSync('menu')

  },

wxss

/* 自定义导航栏 */

.navigation-container {
  position: fixed;
  width: 100%;
  z-index: 99;
  background-color: rgb(28, 190, 96);
}
.navigation-bar {
  position: relative;
  width: 100%;
  display: flex;
  flex-direction: row;
  color: #333333;
}


.nav-title {
  display: flex;
  
  margin-left: 20rpx;
  box-sizing: border-box;
}

.nav-search {
  position: absolute;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}





.bg-logo {
  width: 300rpx;
  display: flex;
  flex-direction: column;
  color: #f7f7f7;
  position: absolute;
  top: 0;
  left:20rpx;
}

踩过的坑

1.误用rpx,因为想着适配一下手机设备高度,就将设备获取到的高度×2,再把单位换成rpx,这会导致“不可预料的事情发生”,后面导航栏那块高度就统一用px了,字体当然也用px了,其实在计算导航栏的时候以及根据设备来计算出px值了,实际上已经适配过一次高度,字体大小另说。

2.系统获取到的胶囊位置,距离右屏幕的值是个未解之谜,理论上是很小的,但是获取到的right大到有几百,本来我是想通过这个属性来计算搜索图标的位置的,right比left还要大就很奇怪。

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
微信小程序中可以通过自定义导航栏实现更加个性化的界面设计。以下是一个简单的实现步骤: 1. 在 app.json 文件中设置 "navigationStyle": "custom",表示使用自定义导航栏。 2. 在页面的 WXML 文件中编写自定义导航栏的 HTML 代码,例如: ``` <view class="navbar"> <view class="navbar-title">自定义导航栏</view> </view> ``` 其中,navbar 表示导航栏的样式,navbar-title 表示标题的样式,可以根据需要进行自定义。 3. 在页面的 wxss 文件中设置导航栏的样式,例如: ``` .navbar { height: 44px; background-color: #fff; border-bottom: 1px solid #ccc; display: flex; align-items: center; justify-content: center; } .navbar-title { font-size: 18px; font-weight: bold; color: #000; } ``` 其中,height 表示导航栏高度,background-color 表示导航栏的背景颜色,border-bottom 表示导航栏底部的边框线,display、align-items 和 justify-content 表示导航栏标题的布局方式,navbar-title 表示标题的样式,可以根据需要进行自定义。 4. 在页面的 JS 文件中设置导航栏的返回按钮,例如: ``` wx.showModal({ title: '提示', content: '确认返回上一页?', success: function (res) { if (res.confirm) { wx.navigateBack({ delta: 1 }) } } }) ``` 其中,wx.showModal 表示显示一个模态框,title 表示模态框的标题,content 表示模态框的内容,success 表示点击确定按钮后执行的回调函数,wx.navigateBack 表示返回上一页。 通过以上步骤,就可以实现微信小程序自定义导航栏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩冉学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值