记录--优雅解决uniapp微信小程序右上角胶囊菜单覆盖问题

这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

前言

大家好,今天聊一下在做uniapp多端适配项目,需要用到自定义导航时,如何解决状态栏塌陷及导航栏安全区域多端适配问题,下文只针对H5、APP、微信小程序三端进行适配,通过封装一个通用高阶组件包裹自定义导航栏内容,主要是通过设置padding来使内容始终保持在安全区域,达到低耦合,可复用性强的效果。

一、创建NavbarWrapper.vue组件

大致结构如下:

<template>
  <view class="navbar-wrapper" :style="{
    paddingTop: statusBarHeight,
    paddingRight: rightSafeArea
  }">
    <slot/>
  </view>
</template>

<script>
  export default {
    name: 'NavbarWrapper',
    data() {
      return {
        // 像素单位
        pxUnit: 'px',
        // 默认状态栏高度
        statusBarHeight: 'var(--status-bar-height)',
        // 微信小程序右上角的胶囊菜单宽度
        rightSafeArea: 0
      }
    }
  }
</script>

<style scoped>
  .navbar-wrapper {
    /**
     * 元素的宽度和高度包括了内边距(padding)和边框(border),
     * 而不会被它们所占据的空间所影响
     * 子元素继承宽度时,只会继承内容区域的宽度 
    */
    box-sizing: border-box;
  }
</style>

目的

主要是动态计算statusBarHeight和rightSafeArea的值。

解决方案

在APP端只需一行css代码即可

.navbar-wrapper {
    padding-top: var(--status-bar-height);
}

下面是关于--status-bar-height变量的介绍:

从上图可以知道--status-bar-height只在APP端是手机实际状态栏高度,在微信小程序是固定的25px,并不是手机实际状态栏高度;

微信小程序时,除了状态栏高度还需要获取右上角的胶囊菜单所占宽度,保持导航栏在安全区域。

以下使用uni.getWindowInfo()uni.getMenuButtonBoundingClientRect()来分别获取状态栏高度和胶囊相关信息,api介绍如下图所示:

主要逻辑代码

在NavbarWrapper组件创建时,做相关计算

created() {
  const px = this.pxUnit
  // #ifndef H5
  // 获取窗口信息
  const windowInfo = uni.getWindowInfo()
  this.statusBarHeight = windowInfo.statusBarHeight + px
  // #endif

  // #ifdef MP-WEIXIN
  // 获取胶囊左边界坐标
  const { left } = uni.getMenuButtonBoundingClientRect()
  // 计算胶囊(包括右边距)占据屏幕的总宽度:屏幕宽度-胶囊左边界坐标
  this.rightSafeArea = windowInfo.windowWidth - left + px
  // #endif
}

用法

<NavbarWrapper>
  <view class="header">header</view>
</NavbarWrapper>

二、多端效果展示

微信小程序

APP端

H5端

三、源码

NavbarWrapper.vue

<template>
  <view class="navbar-wrapper" :style="{
    paddingTop: statusBarHeight,
    paddingRight: rightSafeArea
  }">
    <slot/>
  </view>
</template>

<script>
  export default {
    name: 'NavbarWrapper',
    data() {
      return {
        // 像素单位
        pxUnit: 'px',
        // 默认状态栏高度
        statusBarHeight: 'var(--status-bar-height)',
        // 微信小程序右上角的胶囊菜单宽度
        rightSafeArea: 0
      }
    },
    created() {
      const px = this.pxUnit
      // #ifndef H5
      // 获取窗口信息
      const windowInfo = uni.getWindowInfo()
      this.statusBarHeight = windowInfo.statusBarHeight + px
      // #endif
      
      // #ifdef MP-WEIXIN
      // 获取胶囊左边界坐标
      const { left } = uni.getMenuButtonBoundingClientRect()
      // 计算胶囊(包括右边距)占据屏幕的总宽度:屏幕宽度-胶囊左边界坐标
      this.rightSafeArea = windowInfo.windowWidth - left + px
      // #endif
    }
  }
</script>

<style scoped>
  .navbar-wrapper {
    /**
     * 元素的宽度和高度包括了内边距(padding)和边框(border),
     * 而不会被它们所占据的空间所影响
     * 子元素继承宽度时,只会继承内容区域的宽度 
    */
    box-sizing: border-box;
    background-color: deeppink;
  }
</style>

本文转载于:

https://juejin.cn/post/7309361597556719679

如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniapp微信小程序中,可以使用uni-ui组件库中提供的弹窗组件来实现弹窗右上角添加关闭icon的效果。 首先,在需要使用弹窗的页面中引入uni-ui组件,具体步骤为在pages.json文件中添加如下代码: ```json "easycom": { "autoscan": true, "custom": { "^@dcloudio/uni-ui": "@/uni-ui" } } ``` 然后,在需要使用弹窗的页面中引入弹窗组件,具体步骤为在页面的vue文件中添加如下代码: ```html <template> <view> <uni-popup :show="showPopup" :position="position"> <view>这是弹窗内容</view> <view class="close-icon" @click="closePopup">关闭</view> </uni-popup> </view> </template> <script> import { uniPopup } from '@dcloudio/uni-ui'; export default { components: { uniPopup }, data() { return { showPopup: false, position: 'center' }; }, methods: { openPopup() { this.showPopup = true; // 打开弹窗 }, closePopup() { this.showPopup = false; // 关闭弹窗 } } } </script> <style> .close-icon { position: absolute; top: 0; right: 0; width: 30px; height: 30px; text-align: center; line-height: 30px; background-color: rgba(0,0,0,0.5); color: #fff; cursor: pointer; z-index: 3; } </style> ``` 以上代码中,通过引入uniPopup组件,并使用show属性来控制弹窗的显示与隐藏。通过添加close-icon类及相应的样式,实现右上角的关闭icon,并通过绑定closePopup方法来关闭弹窗。 最后,在需要显示弹窗的事件中,调用openPopup方法来打开弹窗,即可在右上角看到带有关闭icon的弹窗。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值