H5兼容APP ios安全距离 vant

safe-area-inset-top    //顶部安全距离
safe-area-inset-bottom    //底部安全距离

//dom用法
//顶部安全距离
<div class="activityEnt" safe-area-inset-top > </div>
//底部安全距离
<div class="activityEnt" safe-area-inset-bottomtop > </div>

// css用法  要同时写
padding-bottom: constant(safe-area-inset-bottom);                                                     
padding-bottom: env(safe-area-inset-bottom);

可能会需要判断是否需要安全距离

let status = 0; // 0:还没数据,-1:不支持,1:支持

/**
 * 判断当前设置是否支持constant(safe-area-inset-top)或env(safe-area-inset-top);
 * 部分Android设备,可以认识safa-area-inset-top,但会将其识别为0
 * @returns {boolean} 当前设备是否支持安全距离
 */
export const supportSafeArea = () => {
  if (自己项目中的判断方式 == 'android') {    //判断是否是安卓环境            
    if (status !== 0) {
      // 缓存数据,只向 body 插入一次 dom 即可
      return status === 1;
    }
    const div = document.createElement('div');
    const id = 'test-check-safe-area';                  
    const styles = [
      'position: fixed',
      'z-index: -1',
      'height: constant(safe-area-inset-top)',
      'height: env(safe-area-inset-top)',
    ];
    div.style.cssText = styles.join(';');
    div.id = id;
    document.body.appendChild(div);
    const areaDiv = document.getElementById(id);
    if (areaDiv) {
      status = areaDiv.offsetHeight > 0 ? 1 : -1; // 该 div 的高度是否为 0
      areaDiv.parentNode?.removeChild(areaDiv);
    }
    return status === 1;
  }else{
    return true;
  }
};
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

染指悲剧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值