【微信小程序】微信小程序自定义导航栏。(主要针对单个页面复杂导航栏)

第一步:进入小程序先计算高度。

onLaunch() {
  this.setNavSize()
},
// 通过获取系统信息计算导航栏高度
setNavSize: function () {
    var that = this
      , sysinfo = wx.getSystemInfoSync()
      , statusHeight = sysinfo.statusBarHeight
      , isiOS = sysinfo.system.indexOf('iOS') > -1
      , navHeight;
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    this.globalData.statusHeight = statusHeight
    this.globalData.navHeight = navHeight

  },
globalData: {
    screenHeight: wx.getSystemInfoSync().windowHeight,
    statusHeight:'',
    navHeight:'',
    //胶囊尺寸
    MenuButtonBounding_bottom:wx.getMenuButtonBoundingClientRect().bottom,
    MenuButtonBounding_height:wx.getMenuButtonBoundingClientRect().height,
    MenuButtonBounding_left:wx.getMenuButtonBoundingClientRect().left,
    MenuButtonBounding_right:wx.getMenuButtonBoundingClientRect().right,
    MenuButtonBounding_top:wx.getMenuButtonBoundingClientRect().top,
    MenuButtonBounding_width:wx.getMenuButtonBoundingClientRect().width,
  }

第二步:

a.页面js调用。

  data: {
    screenHeight:getApp().globalData.screenHeight,
    statusHeight:getApp().globalData.statusHeight,
    navHeight:getApp().globalData.navHeight,
    MenuButtonBounding_width:getApp().globalData.MenuButtonBounding_width,
    MenuButtonBounding_height:getApp().globalData.MenuButtonBounding_height,
    MenuButtonBounding_top:getApp().globalData.MenuButtonBounding_top,
  },

b.页面json配置(usingComponents引用的是vant2组件,这里是为了更好制作导航栏。引入组件见其他文章)。

{
  "usingComponents": {
    "van-popup": "@vant/weapp/popup/index"
  },
  "navigationStyle":"custom"
}

c.页面view视图(导航栏图标内容自行更改)。

<!-- 导航栏 -->
  <view style="height: {{statusHeight}}px;background-color: #3d5c6e;"></view>
  <view style="height: {{navHeight}}px;background-color: #3d5c6e;">
    <van-icon name="apps-o" slot="left" style="margin-left: 30rpx;margin-top: {{navHeight/5}}px;" color="#fff" size="{{MenuButtonBounding_height/1.3}}px" catchtap="comeBackHome" />
    <image src="data:image/png;base64,{{res_user.image_128}}" style="width: {{MenuButtonBounding_height/1.3}}px;height: {{MenuButtonBounding_height/1.3}}px;position: absolute;right: 0;margin-right: {{MenuButtonBounding_width+15}}px;margin-top: {{navHeight/5}}px;border-radius: 50%;" catchtap="comeBackLogin"  mode="scaleToFill"/>
  </view>

完事!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值