微信小程序自定义 头部 navbar

1.在 app.js 文件中

// app.js
App({
  onLaunch() {
    const that = this;
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync();
    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    // 导航栏高度 = 状态栏高度 + 44
    // (1) 手机端正常,pc端显示有问题
    that.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
    // (2) 手机端,pc端都正常
    that.globalData.navBarHeight = systemInfo.statusBarHeight + menuButtonInfo.height + (menuButtonInfo.top - systemInfo.statusBarHeight) * 2;
    that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    that.globalData.menuTop = menuButtonInfo.top;
    that.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
    that.globalData.menuHeight = menuButtonInfo.height;
  },
  globalData: {
    navBarHeight: 0, // 导航栏高度
    menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
    menuTop: 0, // 胶囊距上方间距
    menuBotton: 0, // 胶囊距底部间距(保持底部间距一致)
    menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  },
});

整个复制进自己项目中去

2.在需要更改自定义 navbar 的 json 文件中修改配置

"navigationStyle": "custom"

3.在需要更改自定义 navbar 的 js 文件中修改配置

data: {
  navBarHeight: app.globalData.navBarHeight,
  menuRight: app.globalData.menuRight,
  menuBotton: app.globalData.menuBotton,
  menuTop: app.globalData.menuTop,
  menuHeight: app.globalData.menuHeight,
}

需要用到那个数据就写哪些,不需要全部都写

4.在需要更改自定义 navbar 的 wxml 文件中修改配置

<view class="top">
    <view class="navbar {{scrollTop>20?'scroll':''}}" style="height: {{  navBarHeight }}px;">
      <view class="navbar-tools" style="height: {{ menuHeight }}px; left:{{menuRight }}px; top:{{menuTop }}px;">
        <view class="search-container" style="border-radius: {{ menuHeight }}px;">
          <van-icon name="search" />
          <input class="searchIpt" type="search" placeholder="请输入搜索关键词" placeholder-style="color: #fff;" style="height: {{  menuHeight }}px;" />
        </view>
      </view>
    </view>
    <swiper class="" indicator-dots indicator-active-color="#1296DB" autoplay interval="2000" circular>
      <swiper-item class="">
        <image class="swiper-img" src="/static/images/swiper/1.jpg" />
      </swiper-item>
      <swiper-item class="">
        <image class="swiper-img" src="/static/images/swiper/2.jpg" />
      </swiper-item>
      <swiper-item class="">
        <image class="swiper-img" src="/static/images/swiper/3.jpg" />
      </swiper-item>
    </swiper>
</view>

这里要设置动态的宽高尺寸

5.在需要更改自定义 navbar 的 wxss 文件中修改配置

.top {
    position: relative;
    background-color: antiquewhite;

    .navbar {
      padding: 0 20rpx;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 80rpx;
      z-index: 9;
      box-sizing: border-box;

      .navbar-tools {
        position: absolute;

        .search-container {
          padding: 0 10rpx;
          display: flex;
          align-items: center;
          background-image: linear-gradient(120deg, #89f7fe 0%, #66a6ff 100%);

          .van-icon {
            margin-right: 10rpx;
            color: #fff;
            font-size: 40rpx;
          }

          .searchIpt {
            color: #fff;
            font-size: 28rpx;
          }
        }
      }
    }
  }

效果图

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈龙龙的陈龙龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值