Skip to content

uniapp获取顶部导航栏高度

  • 业务场景:设置页面容器高度为 100vh - 顶部导航栏的高度

  • 代码示例

    js
    export default {
        data() {
            return {
                // 系统状态栏的高度
                statusBarHeight: 0
            };
        },
        created() {
            this.getSysteminfo();
        },
        methods: {
            // 获取系统栏高度
            getSysteminfo() {
                uni.getSystemInfo({
                    success: res => {
                        this.statusBarHeight = res.statusBarHeight;
                    }
                });
            }
        }
    }
    html
    <view class="nav-bar" :style="{ height: 'calc(100vh - ' + statusBarHeight + 'px)' }">
        <!-- 容器内容 -->
    </view>
  • 注意事项:只有小程序平台能够获取到导航栏的确切高度,对于App和H5平台,通常需要手动设置一个固定的导航栏高度,例如45px

MIT版权,未经许可禁止任何形式的转载