原文连接:JavaScript 判断 iPhone X Series 机型javascript
写在前面
若是有更优雅的方式,必定要告诉我!css
现状
iPhone X 底部是须要预留 34px 的安全距离,须要在代码中进行兼容。java
现状对于 iPhone X 的判断基本是这样的:ios
// h5
export const isIphonex = () => /iphone/gi.test(navigator.userAgent) && window.screen && (window.screen.height === 812 && window.screen.width === 375);
复制代码复制代码
这在以前是没问题的,新的 iPhone X Series 设备发布以后,这个就会兼容就有问题。git
iPhone X Series 参数
机型 | 倍率 | 分辨率 | pt |
---|---|---|---|
iPhone X | 3 | 2436 × 1125 | 812 × 375 |
iPhone XS | 3 | 2436 × 1125 | 812 × 375 |
iPhone XS Max | 3 | 2688 × 1242 | 896 × 414 |
iPhone XR | 2 | 1792 × 828 | 896 × 414 |
width === 375 && height === 812 只能识别出 iPhone X 和 iPhone XS,对于 iPhone XS Max 和 iPhone XR 就无能为力了。github
解决方法
对每一个机型进行判断
const isIphonex = () => {
// X XS, XS Max, XR
const xSeriesConfig = [
{
devicePixelRatio: 3,
width: 375,
height: 812,
},
{
devicePixelRatio: 3,
width: 414,
height: 896,
},
{
devicePixelRatio: 2,
width: 414,
height: 896,
},
];
// h5
if (typeof window !== 'undefined' && window) {
const isIOS = /iphone/gi.test(window.navigator.userAgent);
if (!isIOS) return false;
const { devicePixelRatio, screen } = window;
const { width, height } = screen;
return xSeriesConfig.some(item => item.devicePixelRatio === devicePixelRatio && item.width === width && item.height === height);
}
return false;
}
复制代码复制代码
统一处理方法
由于如今 iPhone 在 iPhone X 以后的机型都须要适配,因此能够对 X 之后的机型统一处理,咱们能够认为这系列手机的特征是 ios
+ 长脸
。web
在 H5 上能够简单处理。安全
const isIphonex = () => {
if (typeof window !== 'undefined' && window) {
return /iphone/gi.test(window.navigator.userAgent) && window.screen.height >= 812;
}
return false;
};
复制代码复制代码
媒体查询
@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
}
@media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 3) {
}
@media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 2) {
}
复制代码复制代码
媒体查询没法识别是否是 iOS,还得加一层 JS 判断,不然可能会误判一些安卓机。iphone