最快速的iphonex适配方案

502 阅读2分钟

iphonex出来了,苹果一出手,业界势必会有变动,这次也一样。iphonex的出现又给大家添堵了。秉着不让用户添堵的原则,我们还是要适配的。
下面说说适配的方案,其实最基本的思路就是避开上下两块区域,只在安全区域显示内容。

最直接的方案

上下使用固定定位,上面用黑色吧44px高度,固定住,下面呢用34px固定住。内容展示方面body也设置一下padding。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>iphonex</title>
    <meta content="width=device-width,maximum-scale=1.0,initial-scale=1.0,minimum-scale=1.0,user-scalable=yes" name="viewport">
    <style>
        body{padding-top:44px;padding-bottom: 34px;}
        .top{position: fixed;width:100%;height:44px;background-color: #000;top:0;left: 0;}
        .bottom{position: fixed;width:100%;height:34px;bottom:0;left: 0;}
    </style>
</head>
<body>
<div class="top"></div>
<div class="content">sfsd</div>
<div class="bottom"></div>
</body>
</html>

进化方案

上面的方式很明显,适合初始开发那么已经做了开发的现有项目应该怎么快速适配呢,根据上面的思路我写了个比较通用的css,只需要将css引入,给body增加这个class就可以了,无需对现有项目进行修改。css中根据分辨率做了过滤,所以不会影响现有的其他设备适配。
iphonex.css

@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
            .iphonex{padding-top:44px;padding-bottom: 34px;}
            .iphonex:before{content:'';display:block;position: fixed;width:100%;height:44px;background-color: #000;top:0;left: 0;z-index:9999;}
            .iphonex:after{content:'';display:block;position: fixed;width:100%;height:34px;bottom:0;left: 0;z-index:9999;}
        }

html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>iphonex</title>
    <meta content="width=device-width,maximum-scale=1.0,initial-scale=1.0,minimum-scale=1.0,user-scalable=yes" name="viewport">
    <link rel="stylesheet" href="iphonex.css">

</head>
<body class="iphonex">
你自己的页面内容
</body>
</html>

注意:如果你页面里面已经存在定位,可能需要自己调整一下,原有元素的定位。