小程序开发:自定义导航栏,兼容适配所有机型(完整案例)

2020-04-2611:01:04APP与小程序开发Comments2,180 views字数 3229阅读模式

大部分情况下我们都是使用微信官方自带的navigationBar配置,但有时候我们需要在导航栏集成搜索框、自定义背景图、返回首页按钮等。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

思路

  • 隐藏官方导航栏
  • 获取胶囊按钮、状态栏相关数据以供后续计算
  • 根据不同机型计算导航栏高度
  • 编写新的导航栏
  • 页面引用自定义导航

正文

隐藏官方导航栏

隐藏导航栏可以全局配置,也可以单独页面配置,具体根据业务需求来。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

小程序开发:自定义导航栏,兼容适配所有机型(完整案例)

全局隐藏文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

//app.json
"window": {
   "navigationStyle": "custom"
}
复制代码

页面隐藏文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

//page.json
{
  "navigationStyle": "custom"
}
复制代码

获取胶囊按钮、状态栏相关数据以供后续计算

公式:导航栏高度 = 状态栏到胶囊的间距(胶囊距上边界距离-状态栏高度) * 2 胶囊高度 状态栏高度。 由公式得知,我们需要获取状态栏高度 胶囊高度 胶囊距上距离文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

注:状态栏到胶囊的间距 = 胶囊到下边界距离。所以这里需要*2文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

小程序开发:自定义导航栏,兼容适配所有机型(完整案例)

状态栏高度

wx.getSystemInfoSync()官方API可以获取系统相关信息,statusBarHeight属性可以获取到状态栏高度文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
复制代码

胶囊高度和胶囊距上边界距离

wx.getMenuButtonBoundingClientRect()官方API可以获取菜单按钮胶囊按钮的布局位置信息。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

小程序开发:自定义导航栏,兼容适配所有机型(完整案例)
const menuButtonInfo = wx.getMenuButtonBoundingClientRect();//胶囊相关信息
const menuButtonHeight = menuButtonInfo.height //胶囊高度
const menuButtonTop = menuButtonInfo.top//胶囊距上边界距离
复制代码

实例

一般情况下,我们需要在运用启动的初始化生命周期钩子进行计算相关的数据,也就是入口文件app.jsonLaunch生命周期钩子文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

//app.js
App({
  onLaunch: function () {
    this.setNavBarInfo()
  },
  
  globalData: {
    //全局数据管理
    navBarHeight: 0, // 导航栏高度
    menuBotton: 0, // 胶囊距底部间距(保持底部间距一致)
    menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
    menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  },

  /**
   * @description 设置导航栏信息
   */
  setNavBarInfo () {
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync();
    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2   胶囊高度   状态栏高度
    this.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2   menuButtonInfo.height   systemInfo.statusBarHeight;
    this.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
    this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    this.globalData.menuHeight = menuButtonInfo.height;
  }
})
复制代码

页面引用自定义导航

//page.wxml
"nav" style="height:{{navBarHeight}}px;">
  
  "capsule-box" style="height:{{menuHeight}}px; min-height:{{menuHeight}}px; line-height:{{menuHeight}}px; bottom:{{menuBotton}}px;">
    "nav-handle">
      "nav-back-icon" src="/images/nav_back.png" bind:tap="navToBackLastPage">
      "nav-home-icon" src="/images/nav_home.png" bind:tap="navToHomePage">
    
    "nav-title">导航标题
  

复制代码
// page.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight,//导航栏高度
    menuBotton: app.globalData.menuBotton,//导航栏距离顶部距离
    menuHeight: app.globalData.menuHeight //导航栏高度
  }

复制代码

封装成组件

我们可能在各自的页面实现不一样的效果,比如在导航栏添加搜索框,日期等,这个时候我们就可以封装一个自定义组件,大大提高我们的开发效率。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

小程序开发:自定义导航栏,兼容适配所有机型(完整案例)
小程序开发:自定义导航栏,兼容适配所有机型(完整案例)

新建component文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

// components/navigation/index.wxml
"nav" style="height:{{navBarHeight}}px;">
  "nav-main">
    
    "capsule-box" 
      style="height:{{menuHeight}}px; min-height:{{menuHeight}}px; line-height:{{menuHeight}}px; bottom:{{menuBotton}}px;left:{{menuRight}}px;"
    >
    
      
    
  

复制代码
// components/navigation/index.wxss
.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%;
}

复制代码
// components/navigation/index.js
const app = getApp()
Component({
  /**
   * 组件的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight, //导航栏高度
    menuRight: app.globalData.menuRight, // 胶囊距右方间距(方保持左、右间距一致)
    menuBotton: app.globalData.menuBotton,
    menuHeight: app.globalData.menuHeight
  }
})
复制代码

页面引用

页面配置引入该自定义组件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

//index.json
{
  "navigationStyle": "custom",
  "navigationBarTextStyle": "white",
  "usingComponents": {
    "navigation": "/components/Navigation/index"
  }
}
复制代码

页面中使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html



  "current-date">
     4月24日
  

复制代码

总结

本文主要是写自定义导航基础的东西,重点在于怎么计算自定义导航的,具体的业务和样式还需要根据自身产品来设定。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

作者:肥仔快乐水
链接:https://juejin.im/post/5e91281fe51d4546f6308cc3
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/18356.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/xcx/18356.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定