微信小程序之沉浸式导航

本文详细介绍了如何在微信小程序中去除默认导航栏并创建自定义沉浸式导航。通过修改.json配置文件,设置WXML结构,使用JS处理动态数据,并应用LESS样式,实现了导航栏的定制。步骤包括设置导航栏高度、添加搜索和标题功能,并在App.js中获取系统信息以适应不同设备。最终实现了一个全屏且具有搜索功能的红色背景导航栏。
摘要由CSDN通过智能技术生成

首先看效果

欢迎大佬前来点评

第一步:在当前 .json( 点json) 文件中粘贴   "navigationStyle": "custom"

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

这一步就是把自带的导航栏去掉

第二步:分别粘贴

WXML

<view class="indexa">
  <view class='nav' style="height:{{customBar}}px">
    <view class='con' style="height:{{customBar}}px;padding-top:{{statusBar}}px;">
      <view class="searchBtn" bindtap="search">
        <i class="iconfont icon-sousuo"></i>
      </view>
      <view class="title" style="top:{{statusBar}}px">首页</view>
    </view>
  </view>
</view>
JS

const app = getApp();
Page({
  data: {
    statusBar: app.globalData.statusBar,
    customBar: app.globalData.customBar,
    custom: app.globalData.custom
  },
LESS(预处理)

// 引入字体图标
@import "../../utils/iconfont/iconfont.wxss";
view, scroll-view, swiper, button, input, textarea, label, navigator, image {
  box-sizing: border-box;
}
.indexa {
  .nav {
    min-height: 0px;
    background: #d20000;
    color: #fff;
    z-index: 9999;
    position: relative;
    .con {
      position: fixed;
      width: 100%;
      top: 0;
      display: flex;
      .searchBtn {
        margin-left: 30rpx;
        font-size: 36rpx;
        display: flex;
        align-items: center;
        height: 100%;
        justify-content: center;
        max-width: 100%;
      }
      .title {
        position: absolute;
        text-align: center;
        width: calc(100% - 340rpx);
        left: 0;
        right: 0;
        bottom: 0;
        top: 0;
        margin: auto;
        height: 60rpx;
        font-size: 32rpx;
        line-height: 60rpx;
        pointer-events: none;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
    }
  }
}
App.js中

    // 获取系统信息
    wx.getSystemInfo({
      success: e => {
        this.globalData.statusBar = e.statusBarHeight; //状态栏高度
        let custom = wx.getMenuButtonBoundingClientRect(); //菜单按钮
        this.globalData.custom = custom;
        this.globalData.customBar = custom.bottom + custom.top - e.statusBarHeight;
        //计算得到定义的状态栏高度
      }
    })

 这样微信小程序——沉浸式导航就做好了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值