h5 底部按钮兼容 iPhone (解决底部横杠遮挡问题)

iOS特性方法(推荐),小程序也可用

padding-bottom: constant(safe-area-inset-bottom); /*兼容 IOS<11.2*/
padding-bottom: env(safe-area-inset-bottom); /*兼容 IOS>11.2*/

<!DOCTYPE html>
<html lang="en">
 
<head>
  <meta charset="utf-8">
  <title>iOS安全区域</title>
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover">
  <style>
    .a{
      position: fixed;
      left: 0;
      bottom: 0;
      width: 100%;
      background-color: red;
      padding-bottom: constant(safe-area-inset-bottom); /*兼容 IOS<11.2*/
      padding-bottom: env(safe-area-inset-bottom); /*兼容 IOS>11.2*/
    }
    .b{
      background-color: green;
      height: 100px;
    }
  </style>
</head>
 
<body>
  <div style="height: 1500px;">Safari浏览器滚动页面查看效果</div>
  <div class="a">
    <div class="b"></div>
  </div>
</body>
 
</html>

方法一:JS判断

function isIphoneX(){
    if(/iphone/gi.test(window.navigator.userAgent)){
        /* iPhone X、iPhone XS */
        var x=(window.screen.width === 375 && window.screen.height === 812);
        /* iPhone XS Max */
        var xsMax=(window.screen.width === 414 && window.screen.height === 896);
        /* iPhone XR */
        var xR=(window.screen.width === 414 && window.screen.height === 896);
        if(x || xsMax || xR){
            return true;
        }else{
            return false;
        }
    }else{
        return false
    }
}
 
 
/* 函数使用 */
if(isIphoneX()){
    document.querySelector( selector ).style.paddingBottom="34px";
}

方法二:CSS媒体查询

@media screen and (width: 375px) and (height: 812px){
    .button {
        padding-bottom: 34px;
    }
}
 
// 更多 iPhone X 系列其它尺寸请参照JS的尺寸写
  1.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值