小程序自定义导航栏

UI设计了一个小程序去掉原生导航栏的样式,要求实现。

开始时我发现全面屏和普通屏效果不一样,这样肯定不行,于是开始寻找解决办法。现已解决,记录如下。

首先,页面的json上肯定要设置"navigationStyle": "custom",用来去掉原生导航栏。

接着新建组件title,title.wxml:

<view class="nav" style="height:{{navBarHeight}}px;">

 <view class="nav-main">

 <!-- 胶囊区域 -->

 <view 

  class="capsule-box"

  style="height:{{menuHeight}}px; min-height:{{menuHeight}}px; line-height:{{menuHeight}}px; bottom:{{menuBotton}}px">

 <!-- 导航内容区域 -->

 <view class="login-name color1_27 font2_36 ">{{name}}</view>

 </view>

 </view>

</view>

title的css:

.nav {

    position: fixed;

    top: 0;

    left: 0;

    width: 100vw;

}



.nav-main {

    width: 100%;

    height: 100%;

    position: relative;

}



.nav .capsule-box {

    position: absolute;

    box-sizing: border-box;

    width: 100%;

}

.login-name{

    width: 100%;

    text-align: center;

    font-size: 36rpx;

  }

title的js,

const util = require('../../utils/util.js');

const api = require('../../config/api.js');

const app = getApp();

Component({

  properties: {

    name:{

      type:String, 

      value:''

    },

  },

  data: {

    navBarHeight: app.globalData.navHight[0], //导航栏高度

    menuBotton: app.globalData.navHight[1],

    menuRight: app.globalData.navHight[2], // 胶囊距右方间距(方保持左、右间距一致)

    menuHeight: app.globalData.navHight[3]

  },

  attached(){

    var this_ = this;



  },

  methods: {

   

  }

})

接着,在app.js里的onlanch里执行这个方法

setNavBarInfo () {

    // 获取系统信息

    const systemInfo = wx.getSystemInfoSync();

    // 胶囊按钮位置信息

    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();

    // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度

    this.globalData.navHight[0] = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight;

    this.globalData.navHight[1] = menuButtonInfo.top - systemInfo.statusBarHeight;

    this.globalData.navHight[2] = systemInfo.screenWidth - menuButtonInfo.right;

    this.globalData.navHight[3] = menuButtonInfo.height;



  },

globalData: {

    navHight:[0,0,0,0], // 导航栏高度,胶囊距底部间距(保持底部间距一致),胶囊距右方间距(方保持左、右间距一致),胶囊高度(自定义内容可与胶囊高度保证一致)

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值