微信小程序自定义头部导航navbar

根据需求,需要做一个自定义的头部导航,以便自适应其他需求和机型,在此封装了一个navbar

wxml文件

<view class='nav-wrap' style='height: {{height*2 + 20}}px; background-color: {{navbarData.backgroundColor}};'>
  <view class='nav-title' style='line-height: {{height*2 + 44}}px; color: {{navbarData.color}}'>{{navbarData.title}}</view>
  <view style='display: flex; justify-content: space-around;flex-direction: column'>
    <view class='nav-capsule' style='height: {{height*2 + 44}}px;' wx:if='{{navbarData.showCapsule}}'>

      <view bindtap='_navback' wx:if='{{!share}}'>
        <image src='/images/back-pre.png' mode='aspectFill' class='back-pre'></image>
      </view>
<!-- 
      <view bindtap='_backhome'>
        <image src='/imgs/back-home.png' mode='aspectFill' class='back-home'></image>
      </view> -->
    </view>
  </view>
</view>

wxss文件

/* 顶部要固定定位   标题要居中   自定义按钮和标题要和右边微信原生的胶囊上下对齐 */
.nav-wrap {
    position: fixed;
    width: 100%;
    top: 0;
    color: #000;
    z-index: 9999999;
  }
  /* 标题要居中 */
  .nav-title {
    position: absolute;
    text-align: center;
    max-width: 400rpx;
    overflow: hidden;
    white-space: nowrap;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 74rpx;
    font-size: 38rpx;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #000000;
    line-height: 50rpx;
  }
  
  .nav-capsule {
    display: flex;
    align-items: center;
    margin-left: 30rpx;
    width: 140rpx;
    justify-content: space-between;
    height: 100%;
  }
  
  .navbar-v-line {
    width: 1px;
    height: 32rpx;
    background-color: #e5e5e5;
  }
  
  .back-pre, .back-home {
    width: 16rpx;
   height: 32rpx;
    margin-top: 6rpx;
    padding: 12rpx;
  }
  .nav-capsule .back-home {
    width: 16rpx;
    height: 32rpx;
    margin-top: 5rpx;
  }

js文件

const app = getApp()
Component({
  properties: {
    navbarData: {   //navbarData   由父页面传递的数据,变量名字自命名
      type: Object,
      value: {},
      observer: function (newVal, oldVal) {}
    }
  },
  data: {
    height: '',
    //默认值  默认显示左上角
    navbarData: {
      showCapsule: 1
    }
  },
  attached: function () {
    // 获取是否是通过分享进入的小程序
    this.setData({
      share: app.globalData.share
    })
    // 定义导航栏的高度   方便对齐
    this.setData({
      height: app.globalData.height
    })
  },
  methods: {
  // 返回上一页面
    _navback() {
      wx.navigateBack()
    }
  //返回到首页
    // _backhome() {
    //   wx.switchTab({
    //     url: '/pages/index/index',
    //   })
    // }
  }

}) 

json文件(定义它为组件)

{
    "component": true
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值