有关HTML5页面在iPhoneX适配问题


Posted in HTML / CSS onNovember 13, 2017

​1.  iPhoneX的介绍
 

屏幕尺寸

我们熟知的iPhone系列开发尺寸概要如下:

有关HTML5页面在iPhoneX适配问题

△ iPhone各机型的开发尺寸

转化成我们熟知的像素尺寸:

有关HTML5页面在iPhoneX适配问题

△ 每个机型的多维度尺寸

倍图其实就是像素尺寸和开发尺寸的倍率关系,但这只是外在的表现。倍图核心的影响因素在于PPI(DPI),了解屏幕密度与各尺寸的关系有助于我们深度理解倍率的概念:《基础知识学起来!为设计师量身打造的DPI指南》

iPhone8在本次升级中,屏幕尺寸和分辨率都遗传了iPhone6以后的优良传统;

然而iPhone X 无论是在屏幕尺寸、分辨率、甚至是形状上都发生了较大的改变,下面以iPhone 8作为参照物,看看到底iPhone X的适配我们要怎么考虑。

我们看看iPhone X尺寸上的变化:

有关HTML5页面在iPhoneX适配问题

2. iPhoneX的适配---安全区域(safe area)

苹果对于 iPhone X 的设计布局意见如下:

有关HTML5页面在iPhoneX适配问题

核心内容应该处于 Safe area 确保不会被设备圆角(corners),传感器外壳(sensor housing,齐刘海) 以及底部的 Home Indicator 遮挡。也就是说 我们设计显示的内容应该尽可能的在安全区域内;

3. iPhoneX的适配---适配方案viewport-fit 3.1 PhoneX的适配,在iOS 11中采用了viewport-fit的meta标签作为适配方案;viewport-fit的默认值是auto。

viewport-fit取值如下:

                                                  auto 默认:viewprot-fit:contain;页面内容显示在safe area内
                                                  cover viewport-fit:cover,页面内容充满屏幕

viewport-fit meta标签设置(cover时)

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

3.2 css constant()函数 与safe-area-inset-top &safe-area-inset-left &safe-area-inset-right &safe-area-inset-bottom的介绍

有关HTML5页面在iPhoneX适配问题

如上图所示 在iOS 11中的WebKit包含了一个新的CSS函数constant(),以及一组四个预定义的常量:safe-area-inset-left,safe-area-inset-right,safe-area-inset-top和safe-area-inset-bottom。当合并一起使用时,允许样式引用每个方面的安全区域的大小。

3.1当我们设置viewport-fit:contain,也就是默认的时候时;设置safe-area-inset-left,safe-area-inset-right,safe-area-inset-top和safe-area-inset-bottom等参数时不起作用的。

3.2当我们设置viewport-fit:cover时:设置如下

body {
    padding-top: constant(safe-area-inset-top);   //为导航栏+状态栏的高度 88px            
    padding-left: constant(safe-area-inset-left);   //如果未竖屏时为0                
    padding-right: constant(safe-area-inset-right); //如果未竖屏时为0                
    padding-bottom: constant(safe-area-inset-bottom);//为底下圆弧的高度 34px       
}

4. iPhoneX的适配---高度统计

viewport-fit:cover + 导航栏

有关HTML5页面在iPhoneX适配问题

5.iPhoneX的适配---媒体查询

注意这里采用的是690px(safe area高度),不是812px;

@media only screen and (width: 375px) and (height: 690px){
    body {
        background: blue;
    }
}

6.iphoneX viewport-fit

问题总结

1.关于iphoneX 页面使用了渐变色时;如果viewport-fit:cover;

1.1在设置了背景色单色和渐变色的区别,如果是单色时会填充整个屏幕,如果设置了渐变色 那么只会更加子元素的高度去渲染;而且页面的高度只有690px高度,上面使用了padding-top:88px;

有关HTML5页面在iPhoneX适配问题

body固定为:

<body><div class="content">this is subElement</div></body>

1.单色时:

* {
           padding: 0;
           margin: 0;        
       }        
       body {
           background:green;
           padding-top: constant(safe-area-inset-top); //88px            
           /*padding-left: constant(safe-area-inset-left);*/            
           /*padding-right: constant(safe-area-inset-right);*/            
           /*padding-bottom: constant(safe-area-inset-bottom);*/        
       }

2.渐变色

* {
           padding: 0;
           margin: 0;
       }
       body {
           background:-webkit-gradient(linear, 0 0, 0 bottom, from(#ffd54f), to(#ffaa22));
           padding-top: constant(safe-area-inset-top); //88px
           /*padding-left: constant(safe-area-inset-left);*/
           /*padding-right: constant(safe-area-inset-right);*/
           /*padding-bottom: constant(safe-area-inset-bottom);*/
       }

解决使用渐变色 仍旧填充整个屏幕的方法;CSS设置如下

有关HTML5页面在iPhoneX适配问题

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="initial-scale=1, viewport-fit=cover">
   <title>Designing Websites for iPhone X: Respecting the safe areas</title>
   <style>        * {
       padding: 0;
       margin: 0;
   }
   html, body {
       height: 100%;
   }
   body {
       padding-top: constant(safe-area-inset-top);
       padding-left: constant(safe-area-inset-left);
       padding-right: constant(safe-area-inset-right);
       padding-bottom: constant(safe-area-inset-bottom);
   }
   .content {
       background: -webkit-gradient(linear, 0 0, 0 bottom, from(#ffd54f), to(#ffaa22));
       width: 100%;
       height: 724px;
   }    </style>
</head>
<body>
<div class="content">this is subElement</div>
</body>
</html>

2.页面元素使用了固定定位的适配即:{position:fixed;}

2.1 子元素页面固定在底部时;使用viewport-fit:contain时;可以看到bottom:0时只会显示在安全区域内;

有关HTML5页面在iPhoneX适配问题

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="initial-scale=1">
   <!--<meta name="viewport" content="initial-scale=1, viewport-fit=cover">-->
   <title>Designing Websites for iPhone X: Respecting the safe areas</title>
   <style>
       * {
           padding: 0;
           margin: 0;
       }
       /*html,body {*/
           /*height: 100%;*/
       /*}*/
       body {
           background: grey;
           /*padding-top: constant(safe-area-inset-top);*/
           /*padding-left: constant(safe-area-inset-left);*/
           /*padding-right: constant(safe-area-inset-right);*/
           /*padding-bottom: constant(safe-area-inset-bottom);*/
       }
       .top {
           width: 100%;
           height: 44px;
           background: purple;
       }
       .bottom {
           position: fixed;
           bottom: 0;
           left: 0;
           right: 0;
           height: 44px;
           color: black;
           background: green;
       }
   </style>
</head>
<body>
   <div class="top">this is top</div>
   <div class="bottom">this is bottom</div>
</body>
</html>

2.1 子元素页面固定在底部时;使用viewport-fit:cover时;可以看到bottom:0时只会显示在安全区域内;

有关HTML5页面在iPhoneX适配问题

添加html,body {width:100%;heigth:100%}

有关HTML5页面在iPhoneX适配问题

图1:

* {
           padding: 0;
           margin: 0;
       }
       html,body {
           height: 100%;
       }
       body {
           background: grey;
           padding-top: constant(safe-area-inset-top);
           padding-left: constant(safe-area-inset-left);
           padding-right: constant(safe-area-inset-right);
           padding-bottom: constant(safe-area-inset-bottom);
       }
       .top {
           width: 100%;
           height: 44px;
           background: purple;
       }
       .bottom {
           position: fixed;
           bottom: 0;
           left: 0;
           right: 0;
           height: 44px;
           color: black;
           background: green;
       }

图2:

* {
           padding: 0;
           margin: 0;
       }
       html,body {
           height: 100%;
       }
       body {
           background: grey;
           padding-top: constant(safe-area-inset-top);
           padding-left: constant(safe-area-inset-left);
           padding-right: constant(safe-area-inset-right);
           /*padding-bottom: constant(safe-area-inset-bottom);*/
       }
       .top {
           width: 100%;
           height: 44px;
           background: purple;
       }
       .bottom {
           position: fixed;
           bottom: 0;
           left: 0;
           right: 0;
           height: 44px;
           color: black;
           background: green;
       }

2.3 关于alertView弹框 遮罩层的解决方案

有关HTML5页面在iPhoneX适配问题

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <!--<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">-->
   <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
   <meta http-equiv="pragma" content="no-cache">
   <meta http-equiv="cache-control" content="no-cache">
   <meta http-equiv="expires" content="0">
   <title>alertView</title>
   <script data-res="eebbk">
       document.documentElement.style.fontSize = window.screen.width / 7.5 + 'px';
   </script>
   <style>
       * {
           margin: 0;
           padding: 0;
       }
       html,body {
           width: 100%;
           height: 100%;
       }
       body {
           font-size: 0.32rem;
           padding-top: constant(safe-area-inset-top);
           padding-left: constant(safe-area-inset-left);
           padding-right: constant(safe-area-inset-right);
           padding-bottom: constant(safe-area-inset-bottom);
       }
       .content {
           text-align: center;
       }
       .testBut {
           margin: 50px auto;
           width: 100px;
           height: 44px;
           border: 1px solid darkgray;
           outline:none;
           user-select: none;
           background-color: yellow;
       }
   </style>
   <link href="alertView.css" rel="stylesheet" type="text/css">
</head>
<body>
   <section class="content">
       <button class="testBut" onclick="showLoading()">弹框加载</button>
   </section>
   <script type="text/javascript" src="alertView.js"></script>
   <script>
       function showLoading() {
           UIAlertView.show({
               type:"input",
               title:"温馨提示",              //标题
               content:"VIP会员即将到期",     //获取新的
               isKnow:false
           });
           var xx = new UIAlertView();
          console.log(xx);
       }
   </script>
</body>
</html>

总结

以上所述是小编给大家介绍的有关HTML5页面在iPhoneX适配问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

HTML / CSS 相关文章推荐
使用CSS3的背景渐变Text Gradient 创建文字颜色渐变
Aug 19 HTML / CSS
css3 media 响应式布局的简单实例
Aug 03 HTML / CSS
使用CSS3实现一个3D相册效果实例
Dec 03 HTML / CSS
利用CSS3实现进度条的两种姿势详解
Mar 21 HTML / CSS
浅谈CSS3特性查询(Feature Query: @supports)功能简介
Jul 31 HTML / CSS
详解HTML5新增标签
Nov 27 HTML / CSS
Html5内唤醒百度、高德APP的实现示例
May 20 HTML / CSS
HTML5标签小集
Aug 02 HTML / CSS
使用Html5多媒体实现微信语音功能
Jul 26 HTML / CSS
HTML中的表单Form实现居中效果
May 25 HTML / CSS
CSS实现章节添加自增序号的方法
Jun 23 HTML / CSS
HTML5+CSS+JavaScript实现捉虫小游戏设计和实现
Oct 16 HTML / CSS
浅谈HTML5 FileReader分布读取文件以及其方法简介
Nov 09 #HTML / CSS
如何避免常见的6种HTML5错误用法
Nov 06 #HTML / CSS
HTML5添加禁止缩放功能
Nov 03 #HTML / CSS
基于HTML5 Canvas的3D动态Chart图表的示例
Nov 02 #HTML / CSS
HTML5新特性之语义化标签
Oct 31 #HTML / CSS
解决HTML5手机端页面缩放的问题
Oct 27 #HTML / CSS
基于HTML5的WebGL实现json和echarts图表展现在同一个界面
Oct 26 #HTML / CSS
You might like
php中heredoc与nowdoc介绍
2014/12/25 PHP
利用php-cli和任务计划实现刷新token功能的方法
2017/05/03 PHP
PHP如何通过date() 函数格式化显示时间
2020/11/13 PHP
自己开发Dojo的建议框架
2008/09/24 Javascript
javascript URL锚点取值方法
2009/02/25 Javascript
javascript实现倒计时N秒后网页自动跳转代码
2014/12/11 Javascript
让编辑器支持word复制黏贴、截屏的js代码
2016/10/17 Javascript
原生JS获取元素集合的子元素宽度实例
2016/12/14 Javascript
微信小程序 常用工具类详解及实例
2017/02/15 Javascript
JS实现列表页面隔行变色效果
2017/03/25 Javascript
Angular 2 ngForm中的ngModel、[ngModel]和[(ngModel)]的写法
2017/06/29 Javascript
ajax跨域访问遇到的问题及解决方案
2019/05/23 Javascript
基于Vue实现电商SKU组合算法问题
2019/05/29 Javascript
Emberjs 通过 axios 下载文件的方法
2019/09/03 Javascript
vue 实现路由跳转时更改页面title
2019/11/05 Javascript
python简单判断序列是否为空的方法
2015/06/30 Python
python 写的一个爬虫程序源码
2016/02/28 Python
python3实现ftp服务功能(客户端)
2017/03/24 Python
浅谈python中的占位符
2017/11/09 Python
Python使用numpy实现BP神经网络
2018/03/10 Python
python pandas dataframe 按列或者按行合并的方法
2018/04/12 Python
TensorFlow的权值更新方法
2018/06/14 Python
python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法
2020/02/14 Python
Python sorted排序方法如何实现
2020/03/31 Python
美国购买当代和现代家具网站:MODTEMPO
2018/07/20 全球购物
马来西亚网上花店:FlowerAdvisor马来西亚
2020/01/03 全球购物
好的演讲稿开场白
2013/12/30 职场文书
澳大利亚商务邀请函
2014/01/17 职场文书
开学典礼感言
2014/02/16 职场文书
运动会开幕式主持词
2014/03/28 职场文书
班组长竞聘书
2014/03/31 职场文书
2014年药品销售工作总结
2014/12/16 职场文书
本科毕业论文导师评语
2014/12/31 职场文书
生日祝酒词大全
2015/08/10 职场文书
利用Python判断你的密码难度等级
2021/06/02 Python
CSS 鼠标选中文字后改变背景色的实现代码
2023/05/21 HTML / CSS