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

业务需求:

ui设计的头部,右边多了一个首页的按钮,效果图

因为基本每个页面都要用得到,所有要做一个自定义的组件,直接上代码

wxml:

<view class="nav-bar" style="padding-top:{{ phoneInfo.safeArea.top}}px;">
    <view class="van-nav-bar" style="height: {{phoneInfo.safeArea.top}}px;">
        <view class="title_view">
            <view class="title_view_left">
                <view bindtap="onClickLeft">
                <!-- 返回图标 -->
                    <van-icon class="van-icon" name="arrow-left" />
                </view>
                <!-- 首页图标 -->
                <view bindtap="onBackHome">
                    <van-icon class="van-icon" name="wap-home-o" />
                </view>
            </view>
            <!-- 标题 -->
            <view class="title_view_right">{{title}}</view>
        </view>
    </view>
</view>

wxss:

/* components/nav-title-bar/nav-title-bar.wxss */

.nav-bar{
    background-color: #f0f0f0;
    display: flex;
    align-items: center;
    position: fixed;
    width: 100%;
    z-index: 99999;
  }
  .van-nav-bar{
    flex: 1;
  }
  /* .van-nav-bar{
    background-color: rgb(66, 104, 107);
    color: white !important;
    
  } */
  .title_view{
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    background-color: #f0f0f0;
    position: relative;
  }
  .title_view_left{
    display: flex;
    position: absolute;
    left: 15rpx;
  }
  .title_view_left view{
    margin-right: 7rpx;
    font-size: 45rpx;
    color: #090909;
  }
  /* .van-nav-bar view{
    background-color: #f0f0f0;
    color: white !important;
  } */
  .title_view_right{
    font-size: 30rpx;
    color: #090909;
  }
  .custom-class{
    padding-top: 0 !important;
  }
  .title_view_left .van-icon{
    font-size: 30rpx;
  }

js:

// components/nav-title-bar/nav-title-bar.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        showLeftBack: {
          type: Boolean,
          value:false
        },
        title:{
          type:String,
          value:""
        }
      },

    /**
     * 组件的初始数据
     */
    data: {
        phoneInfo: wx.getSystemInfoSync(), //获取导航栏的高度
        capsuleButton:wx.getMenuButtonBoundingClientRect()
    },


    /**
     * 组件的方法列表
     */
    methods: {
      // 返回上一页
      onClickLeft() {
        wx.navigateBack({
          delta:1
        })
      },
      // 回到首页
      onBackHome(){
        wx.reLaunch({
          url: '/pages/index/index'
        })
      }
    }
})

父组件使用的时候:

<nav-title-bar showLeftBack="{{true}}" title="工单管理" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值