微信小程序获取胶囊高度wx.getMenuButtonBoundingClientRect()

写在app.js里

App({
    onLaunch() {
        // 自定义头部
        let menuButtonObject = wx.getMenuButtonBoundingClientRect();
        wx.getSystemInfo({
            success: res => {
                // 导航高度
                let statusBarHeight = res.statusBarHeight,
                    navTop = 0,
                    navBottom = 0,
                    // 胶囊按钮与右侧的距离=windowWidth-right+胶囊宽度
                    navObjWid = res.windowWidth - menuButtonObject.right + menuButtonObject.width,
                    navHeight = menuButtonObject.height + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
                // navHeight = res.system;
                if (res.system.toLowerCase()?.indexOf('android') >= 0 || res.system.toLowerCase()?.indexOf('ios') >= 0) {
                    navTop = menuButtonObject.top;
                    navBottom = (menuButtonObject.bottom + 10);
                }
                this.globalData.navTop = navTop; //胶囊距离顶部距离
                this.globalData.navHeight = navHeight; //胶囊高度
                this.globalData.navBottom = navBottom; //导航栏底部高度
                this.globalData.navObj = menuButtonObject.height; //胶囊高度
                this.globalData.navObjWid = navObjWid; //胶囊宽度(包括右边距离)
                this.globalData.windowWidth = res.windowWidth;
                this.globalData.windowHeight = res.windowHeight;
                // console.log(res,this.globalData,"0000000000000000000")
            },
            fail(err) {
                console.log(err)
            }
        })
      
    },
       globalData: {}
})

在你想用的页面引入

wxml:

<view class="header" style="margin-top: {{navTop}}px;">
    <view style="margin:10px 0;">
        <view style="padding-right:3%;">
            <view class="head-back flex-row j_b" style="width: {{windowWidth-navObjWid-20}}px;">
                <input type="text" placeholder="请输入知识关键字" class="input" bindinput="bindNames" value="{{name}}" placeholder-style="color:#999;opacity:0.8;font-size:28rpx;"/>
                <view style="color: #999;" bind:tap="getList">搜索</view>
            </view>
        </view>
    </view>
</view>

js:

const app = getApp();
Page({
  data: {
    // 导航栏高度
    navHeight: app.globalData.navHeight,
    // 导航栏距离顶部距离
    navTop: app.globalData.navTop,
    // 胶囊的高度
    // navObj: app.globalData.navObj,
    // 胶囊宽度+距右距离
    navObjWid: app.globalData.navObjWid,
    windowWidth:app.globalData.windowWidth,
    ...
  },
})

json:

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

wxss:

page {
    width: 100%;
    background: #f9f9f9;
  }
  .header {
    width: 100%;
    z-index: 1000;
  }
  
  .header .header-bg {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    clip-path: ellipse(95% 50% at 50% 35%);
  }
  
  .head-back {
    padding: 0 20rpx;
    box-sizing: border-box;
    height: 60rpx;
    border-radius: 50rpx;
    border: 1rpx solid rgba(204, 204, 204, .6);
    /* width: calc(100% - 250rpx); */
    /* calc(100%-{{navObjWid+200}})px */
    width: 73%;
    margin-left: 25rpx;
    position: relative;
    display: flex;
    align-items: center;
  }
  
 
  
  .input{
    height: 60rpx;
    width: 82%;
  }

### 获取微信小程序顶部导航栏高度的方法 为了准确获取微信小程序中的顶部导航栏高度,可以利用 `wx.getSystemInfo` 或者 `wx.getMenuButtonBoundingClientRect` API 来动态计算不同设备上的具体数值[^1]。 对于大多数场景而言,通过调用微信提供的接口来获得系统信息是一种有效的方式。下面是一个具体的实现方法: #### 方法一:使用 wx.getSystemInfoSync() 此同步函数能够立即返回系统的相关信息对象,其中包括了窗口的高度、宽度以及状态栏的高度等属性。基于这些数据再结合特定公式可推导出导航栏的整体尺寸[^2]。 ```javascript const systemInfo = wx.getSystemInfoSync(); // 导航栏高度由多个部分组成 let navBarHeight = (systemInfo.statusBarHeight + 44); // 这里假设胶囊按钮下方至导航栏底边的距离固定为44px console.log('navigation bar height:', navBarHeight); ``` #### 方法二:使用 wx.getMenuButtonBoundingClientRect() 这种方法更为精确,因为它直接提供了菜单按钮(即左上角的胶囊按钮)的位置和大小信息,从而允许开发者更灵活地调整布局以适应不同的屏幕比例和设计需求[^3]。 ```javascript function getNavHeight() { const rect = wx.getMenuButtonBoundingClientRect(); let statusBarHeight; try { statusBarHeight = wx.getSystemInfoSync().statusBarHeight; } catch(e){ console.error("Failed to fetch status bar height", e); return null; } const navigationBarHeight = rect.bottom - rect.top + statusBarHeight; console.log(`Navigation Bar Height is ${navigationBarHeight}px`); } getNavHeight(); ``` 以上两种方式都可以帮助开发人员有效地测量并应用正确的导航栏高度值,在实际项目中可以根据具体情况选择最合适的一种或组合使用两者的结果来进行优化处理[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值