mpvue小程序自定义导航栏


mpvue小程序自定义导航栏

1. 在要用自定义导航的页面的json文件中启用navigationStyle

{
  "usingComponents": {
  },  
  "navigationStyle": "custom"
}

2. 将自定义导航设计为一个组件

navigation-top组件中:

<template>
  <div class="container">
    <!-- 手机状态栏高度 -->
    <div :style="{height:statusBarHeight +'px'}"></div>
    <!-- 导航栏高度 -->
    <div class="navWarp" :style="{height: navBarHeight +'px',lineHeight: navBarHeight +'px'}">
      <img class="kefu" src="../../static/images/other/kefu.png" @click="contact" alt="">
      <p class="title">{{title}}</p> 
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: String
  },
  data() {
    return {
      navBarHeight: 0, // 导航栏高度
      statusBarHeight: 0 ,// 手机状态栏高度
    }
  },
  created() {
    let systemInfo = wx.getSystemInfoSync();
    this.statusBarHeight = systemInfo.statusBarHeight
    let rect = wx.getMenuButtonBoundingClientRect ? wx.getMenuButtonBoundingClientRect() : null; //胶囊按钮位置信息
    wx.getMenuButtonBoundingClientRect();
    this.navBarHeight = ( _ => { //导航栏高度
      let gap = rect.top - this.statusBarHeight; //动态计算每台手机状态栏到胶囊按钮间距
      return 2 * gap + rect.height;
    })();
    console.log(systemInfo,this.navBarHeight)
  },
  methods: {
    contact(){

    },
  },
}
</script>

<style lang="less" scoped>
  .container{
    width: 100%;
    background:#fff;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 999;
    .navWarp{
      position: relative;
      .title{
        font-weight: 600;
        font-size: 34rpx;
        text-align: center;
        /* max-width: 400rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap; */
      }
      .kefu{
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        left: 28rpx;
        width: 52rpx;
        height: 52rpx;
        border: 2rpx #ccc solid;
        border-radius: 50%;
      }
    }
  }
</style>

3. 引入自定义导航组件

<!-- 自定义导航组件 -->
<navigation-top ref="navigation" title="溢涌堂"/>

文章作者: Mr. Zhan
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Mr. Zhan !
 上一篇
vue自定义组件使用v-model指令实现dialog组件的二次封装 vue自定义组件使用v-model指令实现dialog组件的二次封装
vue自定义组件使用v-model指令实现dialog组件的二次封装当某个组件是对element的dialog组件进行二次封装时,我们需要对dialog进行隐藏显示,当子组件里的dialog关闭时,需要修改父组件传入的值,尽管不是表单组件也
2021-01-13 Mr. Zhan
下一篇 
小程序和H5支付 小程序和H5支付
一、小程序支付流程: 调接口获取用户token 1 button按钮中绑定open-type="getUserInfo", @getuserinfo="getuserinfo" 在绑定事件回调中获取
2020-11-12 Mr. Zhan
  目录