美文网首页
H5 iPhoneX全面屏适配方案

H5 iPhoneX全面屏适配方案

作者: 南城FE | 来源:发表于2020-07-14 09:56 被阅读0次

    一、media query方式

    
    /*iPhone X 适配*/
    @media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
     .fixed-bottom{
     bottom: 37px;
     }
    }
    /*iPhone XS max 适配*/
    @media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio:3) {
     .fixed-bottom{
     bottom: 37px;
     }
    }
    /*iPhone XR max 适配*/
    @media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio:2) {
     .fixed-bottom{
     bottom: 37px;
     }
    }
    

    二、CSS函数

    meta标签加入viewport-fit=cover

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
    

    css写法,不支持env、constant的浏览器会忽略此样式

    .fixed-bottom{
     bottom: 0;
     bottom: constant(safe-area-inset-bottom);
     bottom: env(safe-area-inset-bottom);
    }
    

    三、JS判断

    if($(window).width() === 375 && $(window).height() === 724 && window.devicePixelRatio === 3){
        //
    }
    

    解决Iphonex 底部按钮fixed,bottom:0 底部留白问题因为本身页面内容的高度不够, 需通过代码撑开元素的高度.

    1. 配合viewport-fit=“cover”使用
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1,maximum-scale=1, user-scalable=0,viewport-fit=cover">
    

    Demo

    <div class="wrapper">
        <div class="main-wrapper">我是中间区域</div>
        <div class="btn-wrapper">我是fixed按钮</div>
    </div>
    // css 区域
    .wrapper{
        position: relative;
        padding-bottom: 100px;
        box-sizing: border-box;
    }
    .main-wrapper{
        overflow: auto;
    }
    .btn-wrapper{
        width: 100%;
        height: 100px;
        position: fixed;
        left: 0;
        bottom: 0;
    }
    // iphonex 系列 需增加样式
    //iphoneX、iphoneXs
    @media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
        .wrapper{
            height: 100vh;
            overflow: hidden;
        }
        .main-wrapper{
            height: 100%;
        }
    }
    //iphone Xs Max
    @media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio:3) {
        .wrapper{
            height: 100vh;
            overflow: hidden;
        }
        .main-wrapper{
            height: 100%;
        }
    }
    //iphone XR
    @media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio:2) {
        .wrapper{
            height: 100vh;
            overflow: hidden;
        }
        .main-wrapper{
            height: 100%;
        }
    }
    

    相关文章

      网友评论

          本文标题:H5 iPhoneX全面屏适配方案

          本文链接:https://www.haomeiwen.com/subject/slkxhktx.html