头图

用于做底部悬浮定位,悬浮在tabbar上
在设置tabbar的组件中获取,利用组件生命周期,获取tabbar高度,将高度存到本地,用于全局获取

    attached() {
            let query = wx.createSelectorQuery().in(this);
            query.select('.custom-tab-bar').boundingClientRect(rect => {
                console.log('获取tabBar元素的高度', rect);
                wx.setStorageSync('tabBarHeight', rect.height) // 将获取到的高度设置缓存,以便之后使用
            }).exec();
    },

页面中使用,将元素固定到tabbar上方

            <view :style="{
                width: '100%',
                position: 'fixed',
                bottom: tabbarHeight + 'px',
            }"></view>
export default {
        data() {
            return {
                tabbarHeight: 0,
            }
        },
      onLoad() {
      this.tabbarHeight = uni.getStorageSync('tabBarHeight') || 0;
    }
}

这样页面就可以动态获取tabbar的高度


兔子先森
466 声望556 粉丝

致力于新技术的推广与优秀技术的普及。