uniapp开发微信小程序自定义顶部导航栏

文章讲述了在小程序中如何进行自定义导航栏高度的计算,以保持其固定在头部。通过获取系统状态栏高度和菜单按钮的布局信息,计算出导航栏和状态栏的总高度,从而实现全屏页面下导航栏的正确显示。同时,代码示例展示了如何处理胶囊位置信息和内容区域的顶部margin。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

⼩程序⾃定义导航栏⾼度计算
⾃定义导航栏后页⾯就成了全屏状态,如果还要⾃定义导航栏,那么就需要重新计算导航栏的⾼度,让其⼀直 fixed 在头部。

    <!-- 自定义导航栏 -->
    <view class="top_scroll">
      <view class="nav" :style="topx">
        <!-- 自定义导航栏 -->
        <view class="status_bar" :style="{ height: statusBarHeight + 'px' }">
          <!-- uni-ui这里是状态栏 -->
        </view>
        <!-- 胶囊位置信息 -->
        <view
          class="nav_content"
          :style="{height:navBarHeight+'px;line-height:'+navBarHeight+'px'}"
        >
          <view class="nav_left" @click="selectCity">
            <text v-if="currentCity.districtCode > 0">{{
              currentCity.district
            }}</text>
            <text v-else-if="currentCity.cityCode > 0">{{
              currentCity.city
            }}</text>
            <text v-else-if="currentCity.provinceCode > 0">{{
              currentCity.province
            }}</text>
            <text v-else>暂无 </text>
            <image
              class="top3xia"
              src="../../static/arrow_down.png"
              mode="widthFix"
            ></image>
          </view>
          <view class="nav_title">{{title}} </view>
        </view>
      </view>
    </view>
  //第一次加载时调用
  created() {
    if (wx.canIUse("getMenuButtonBoundingClientRect")) {
      let sysInfo = wx.getSystemInfoSync(); //状态栏的高度
      console.log("sysInfo", sysInfo);
	  // 获取状态栏高度
      this.statusBarHeight = sysInfo.statusBarHeight;
      // 胶囊位置信息
      let rect = wx.getMenuButtonBoundingClientRect();
      this.menuButtonRect = JSON.parse(JSON.stringify(rect));
      // 导航栏高度
      let navBarHeight = (rect.top - sysInfo.statusBarHeight) * 2 + rect.height;
      this.navBarHeight = navBarHeight;
      // 自定义导航栏的高度
      this.height = sysInfo.statusBarHeight + navBarHeight;
      // 计算状态栏与导航栏的总高度
      var customBar = this.navBarHeight+this.statusBarHeight
      this.topx = "height:" + customBar + "px";
   
      // 底部排除自定义导航的高度
      const Bottom = (sysInfo.screenHeight - sysInfo.safeArea.bottom) * 2 + 120;
      const marTop = customBar + 5;
      this.marginTop = "margin-top:" + marTop + "px;display: inline-block;";
      this.paddingBottom = "padding-bottom: " + Bottom + "rpx";
    } else {
      wx.showToast({
        title: "您的微信版本过低,界面可能会显示不正常",
        icon: "none",
        duration: 4000,
      });
    }
  },
.top_scroll {
  position: fixed;
  width: 100%;
  z-index: 10000;
}

.nav {
  /* background-color: blue; */
  background-image: linear-gradient(
    180deg,
    rgba(87, 149, 248, 1),
    rgba(179, 211, 254, 1)
  );
  min-height: 50rpx;
  position: relative;

  .nav_content {
    position: relative;
    // border: solid 1px #f00;

    .nav_title {
      position: absolute;
      bottom: 0;
      width: 100%;
      text-align: center;
      // height: 80rpx;
      // line-height: 80rpx;
      font-size: 30rpx;
      font-weight: 1000;
      color: #fff;
    }

    .nav_left {
      position: absolute;
      bottom: 0;
      width: 25%;
      z-index: 10;
      color: #fff;
      left: 20rpx;
      text-align: left;
      font-size: 30rpx;
    }

    .top3xia {
      width: 20rpx;
      margin-left: 12rpx;
    }
  }
}
### 如何在 UniApp开发微信小程序自定义菜单栏 #### 背景说明 UniApp 是一个多端框架,支持一次编写代码并部署到多个平台(如微信小程序、H5 和 App)。通过 UniApp 开发微信小程序时,可以利用其丰富的模板和功能来实现复杂的界面设计,比如自定义菜单栏。 为了实现在 UniApp开发微信小程序自定义菜单栏,以下是详细的解决方案: --- #### 实现步骤概述 1. **配置 `pages.json` 文件** 需要在项目的 `pages.json` 文件中启用自定义导航栏的功能。具体来说,需要设置 `"navigationStyle": "custom"` 属性[^1]。 2. **HTML 结构** 使用 Vue 的组件化结构,在页面顶部放置一个自定义菜单栏容器,并绑定事件处理逻辑。 3. **样式调整** 利用 CSS 对菜单项进行布局和美化,确保适配不同屏幕尺寸。 4. **交互逻辑** 编写 JavaScript 或 TypeScript 代码,用于响应用户的点击行为,并触发相应的业务逻辑。 --- #### 示例代码 ##### 1. 修改 `pages.json` ```json { "globalStyle": { "navigationBarTitleText": "自定义菜单栏", "navigationStyle": "custom" }, "pages": [ { "path": "pages/index/index", "style": { "navigationStyle": "custom" } } ] } ``` ##### 2. 页面 HTML (Vue 组件) ```vue <template> <view class="menu-bar"> <view class="menu-item" @click="handleItemClick('home')">首页</view> <view class="menu-item" @click="handleItemClick('profile')">个人中心</view> <view class="menu-item" @click="handleItemClick('settings')">设置</view> </view> </template> <script> export default { methods: { handleItemClick(pageName) { console.log(`跳转至 ${pageName}`); switch (pageName) { case 'home': this.$router.push('/pages/home/home'); break; case 'profile': this.$router.push('/pages/profile/profile'); break; case 'settings': this.$router.push('/pages/settings/settings'); break; default: console.error('未知页面:', pageName); } } } }; </script> <style scoped> .menu-bar { display: flex; justify-content: space-around; align-items: center; height: 80px; background-color: #f7f7f7; border-bottom: 1px solid #eaeaea; } .menu-item { font-size: 16px; color: #333; padding: 8px 16px; cursor: pointer; } .menu-item:hover { background-color: #ddd; } </style> ``` --- #### 关键点解析 - **自定义导航栏** 在微信小程序中,默认提供了一个固定的导航栏,但如果希望完全控制导航栏的内容和样式,则可以通过设置 `"navigationStyle": "custom"` 来隐藏默认导航栏,并自行绘制替代方案。 - **路由管理** 上述示例中使用了 `$router.push()` 方法来进行页面跳转。这是基于 Vue Router 的机制,适用于 UniApp 项目中的多页应用开发场景。 - **跨端兼容性** 如果计划将此功能扩展到其他平台(如 H5 或 App),需要注意某些样式可能因设备差异而表现不一致。因此建议测试多种环境下的显示效果[^2]。 --- #### 注意事项 - 确保已安装最新版本的 HBuilderX 工具,以便获得最佳的开发体验和支持。 - 测试过程中需关注性能优化问题,尤其是当菜单栏包含大量动态数据或复杂动画时。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值