2026最新 · 无废话代码片段

微信小程序吸顶导航栏 + 状态栏高度自适应(自定义顶栏)

精准获取胶囊按钮与状态栏高度,完美适配所有刘海屏/灵动岛机型。

WXML 模板

<view style="padding-top: {{statusBarHeight}}px; height: {{navBarHeight}}px; position: sticky; top: 0; background: #fff; display: flex; align-items: center; padding-left: 24rpx;">
  <text style="font-weight: 700;">自定义吸顶标题</text>
</view>

JS 逻辑代码

Page({
  data: { statusBarHeight: 20, navBarHeight: 44 },
  onLoad() {
    const { statusBarHeight } = wx.getWindowInfo();
    const menu = wx.getMenuButtonBoundingClientRect();
    const navBarHeight = (menu.top - statusBarHeight) * 2 + menu.height;
    this.setData({ statusBarHeight, navBarHeight });
  },
});
需要开箱即用的完整过审小程序项目?内置隐私弹窗、激励视频防刷与标准页面结构,提审直接通过。
查看 /templates 源码库 →