[转] iphoneX、iphoneXS、iphoneXSMax、iphoneXR适配
基础知识
1. 关于iphoneX 、iphoneXS、iphoneXSMax、iphoneXR机型的大小和像素
![](http://upload-images.jianshu.io/upload_images/5393165-973cc636d2030bbc.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
机型尺寸
注意:开发人员只需要记住开发尺寸
2. 屏幕组成
齐刘海(44px) + 安全区域 + 手势区域(34px)
![](http://upload-images.jianshu.io/upload_images/5393165-c78594308b9b539a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/767/format/webp)
屏幕组成
适配方案 1. viewport-fit viewport-fit="contain"展示区域在安全区中,不包括齐刘海和底部手势区域 viewport-fit="cover"展示区域整个屏幕中,包括齐刘海和底部手势区域 所以使用viewport-fit="contain"就可以解决适配问题,将下面的代码放在<head>标签中。 <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=contain"> 2. css媒体查询 X、XS机型的媒体查询 /* x xs */ @media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) { // iphoneX iphoneXS样式 } XR机型媒体查询 /* xr */ @media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 2) { // iphoneXR样式 } XS MAX机型媒体查询 /* xs max */ @media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 3) { // iphoneXR样式 } 3. js判断 通过window.navigator.userAgent、window.devicePixelRatio、window.screen三个属性来匹配 X、XS机型js检查 let isIphoneX = (/iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 375 && window.screen.height === 812) XR机型js检查 let isIphoneXr = (/iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 2 && window.screen.width === 414 && window.screen.height === 896) XS MAX机型js检查 let isIphoneXsMax = (/iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 414 && window.screen.height === 896)