在uniapp中,微信小程序获取胶囊的高度、宽度,且适配微信小程序、App

效果:

H5:

微信小程序:

方法:

 function getWXStatusHeight() {
    // 获取距上
    const barTop = wx.getSystemInfoSync().statusBarHeight
    // 获取胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
    // 获取导航栏高度
    const barHeight = menuButtonInfo.height + (menuButtonInfo.top - barTop) * 2
    let barWidth = menuButtonInfo.width


    return {
        barHeight,
        barTop,
        barWidth
    }

}

export {
    getWXStatusHeight
}

使用:

1、引入:

import {
        getWXStatusHeight
    } from '@/common/util.js'

2、

<!-- #ifdef MP-WEIXIN -->
        <view class="nav" :style="padding">
            <!-- <uni-nav-bar  left-icon="left" right-icon="cart" @clickLeft="back" title="标题" :height="barHeight + 'px'" /> -->
            <uni-nav-bar :height="barHeight + 'px'" left-icon="left" @clickLeft="back" :barWidth="barWidth + 10"
                title="信息中心" rightWidth="150rpx">
                <template v-slot:right>
                    <view class="cilck_all">
                        <text class="iconfont icon-chakanmima"></text>
                        <text class="has_read">一键已读</text>
                    </view>
                </template>
            </uni-nav-bar>
        </view>
        <!-- #endif -->
        <!-- #ifdef APP-PLUS || H5 -->
        <view class="nav">
            <uni-nav-bar left-icon="left" @clickLeft="back" title="信息中心" rightWidth="200rpx">
                <template v-slot:right>
                    <!-- <text class="iconfont icon-bianjishuru" @click="edit_department"></text> -->
                    <!-- <text class="iconfont icon-chakanmima"></text> -->
                    <view class="cilck_all">
                        <text class="iconfont icon-chakanmima"></text>
                        <text class="has_read">一键已读</text>
                    </view>
                </template>
            </uni-nav-bar>
        </view>
        <!-- #endif -->

data() {
            return {
                // bar (title这一条) 距
                barTop: 0,
                // bar 高度, bar 是 fixed
                barHeight: 0,
                // 胶囊的宽度
                barWidth: 0,
                // 获取到的小程序中胶囊的位置信息
                // #ifdef MP-WEIXIN
                getHeight: getWXStatusHeight(),
                // #endif
                padding: {}
            }
        },
        onLoad() {
            // #ifdef MP-WEIXIN
            let obj = this.getHeight
            this.barTop = obj.barTop
            this.barHeight = obj.barHeight
            this.barWidth = obj.barWidth
            this.padding = {
                'padding-top': this.barTop + 'px',

            }
            // #endif

        },
    .nav {
        /* #ifdef APP-PLUS || H5 */
        padding-top: var(--status-bar-height);
        /* #endif */
        background-color: #fff;

        .cilck_all {
            font-size: 18rpx;
            border: 1rpx solid black;
            padding: 10rpx 14rpx;
            border-radius: 30rpx;
            width: 200rpx;
            text-align: center;

            text {
                font-size: 18rpx;
            }

            .has_read {
                margin-left: 10rpx;
            }
        }
    }

### 如何在 UniApp获取小程序右上角胶囊菜单位置 在 UniApp 开发中,可以通过调用微信提供的 `wx.getMenuButtonBoundingClientRect` 接口来获取小程序右上角胶囊按钮(菜单按钮)的位置信息。此接口返回的是胶囊按钮的布局位置信息对象,包括宽度高度以及相对于屏幕边缘的距离。 以下是具体的实现方法: #### 使用示例代码 ```javascript export default { mounted() { this.getCapsulePosition(); }, methods: { getCapsulePosition() { const menuButtonInfo = wx.getMenuButtonBoundingClientRect(); // 调用 API 获取胶囊按钮位置 console.log('胶囊按钮位置:', menuButtonInfo); // 输出具体参数 console.log(`顶部距离: ${menuButtonInfo.top}px`); console.log(`底部距离: ${menuButtonInfo.bottom}px`); console.log(`左侧距离: ${menuButtonInfo.left}px`); console.log(`右侧距离: ${menuButtonInfo.right}px`); console.log(`宽度: ${menuButtonInfo.width}px`); console.log(`高度: ${menuButtonInfo.height}px`); // 可以基于这些数据动态调整页面样式或其他逻辑操作 } } }; ``` 上述代码展示了如何通过 `wx.getMenuButtonBoundingClientRect` 方法获取胶囊按钮的具体位置,并将其打印到控制台以便观察[^1]。 需要注意的是,在某些场景下可能需要考虑不同机型之间的差异性,因为胶囊按钮的实际尺寸和位置会因设备而异。因此建议始终依赖于实际获取的数据而非固定值来进行适配设计[^3]。 另外值得注意的一点是,如果是在 H5 或其他非微信环境运行,则该 API 不可用;它仅适用于微信小程序环境中使用[^4]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值