小程序自定义导航栏

1.设置 “navigationStyle”:“custom”,

{
                    "path" : "business_circle/business_circle",
                    "style" : {
                        "navigationStyle":"custom",
                        "app-plus" : {
                            "bounce" : "none" // 取消iOS回弹,避免和下拉刷新冲突 (小程序无法取消, 点此查看H5端的                      取消方法... )
                            "titleNView": false   
                        }
                    }
                },

获取到导航栏的高度

onst {
        statusBarHeight,   //获取状态栏的高度
        platform           //获取客户端平台
      } = wx.getSystemInfoSync()
 const {
        top,     //按钮胶囊距离顶部的高度
        height   //获取胶囊按钮的高度
      } = wx.getMenuButtonBoundingClientRect()
 // 状态栏高度
      wx.setStorageSync('statusBarHeight', statusBarHeight)
      // 胶囊按钮高度 一般是32 如果获取不到就使用32
      wx.setStorageSync('menuButtonHeight', height ? height : 32)

 // 判断胶囊按钮信息是否成功获取
      if (top && top !== 0 && height && height !== 0) {
        const navigationBarHeight = (top - statusBarHeight) * 2 + height
        // 导航栏高度
        wx.setStorageSync('navigationBarHeight', navigationBarHeight)
      } else {
        wx.setStorageSync(
          'navigationBarHeight',
          platform === 'android' ? 48 : 40
        )
      }

在页面获取高度

data(){
     return{
       // 状态栏高度
          statusBarHeight: wx.getStorageSync('statusBarHeight') + 'px',
          // 导航栏高度
          navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',
          // 胶囊按钮高度
          menuButtonHeight: wx.getStorageSync('menuButtonHeight') + 'px',
          // 导航栏和状态栏高度
          navigationBarAndStatusBarHeight:
            wx.getStorageSync('statusBarHeight') +
            wx.getStorageSync('navigationBarHeight') +
            'px'
     }
    },

使用高度

<template>
    <view >
      <view class="navigation-container" :style="{'height':  navigationBarAndStatusBarHeight}">
        <!--空白来占位状态栏-->
        <view :style="{'height': statusBarHeight}"></view>
        <!--自定义导航栏-->
        <view class="navigation-bar" :style="{'height': navigationBarHeight}">
          <view class="navigation-buttons" :style="{'height': menuButtonHeight}" @click="pagegoback">
            <image class="nav-img" src='../static/img/goback.png'/>
          </view>
          <view class="navigation-title" :style="{'height': navigationBarHeight}">{{name}}</view>
        </view>
      </view>
      <!--空白占位fixed空出的位置-->
      <view :style="{'height':navigationBarAndStatusBarHeight}" style=" background: #ffffff"></view>
    </view>
</template>

样式

.navigation-container {
  position: fixed;
  width: 100%;
  z-index: 99;
  top: 0;
  left: 0;
  background-color: #ffffff;
}
.navigation-bar {
  position: relative;
  display: flex;
  flex-direction: row;
  align-items: center;
}
.navigation-buttons {
  display: flex;
  align-items: center;
  margin-left: 10px;
  box-sizing: border-box;
  background-color: transparent;
}
.nav-img{
  height: 32rpx;
  width: 32rpx;
}
.navigation-title {
  position: absolute;
  left: 104px;
  right: 104px;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  color: #000000;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值