最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript 判斷iPhone X Series機(jī)型的方法

 更新時間:2019年01月28日 10:26:37   作者:axuebin  
這篇文章主要介紹了JavaScript 判斷iPhone X Series機(jī)型的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

寫在前面

如果有更優(yōu)雅的方式,一定要告訴我!

現(xiàn)狀

iPhone X 底部是需要預(yù)留 34px 的安全距離,需要在代碼中進(jìn)行兼容。

現(xiàn)狀對于 iPhone X 的判斷基本是這樣的:

// h5
export const isIphonex = () => /iphone/gi.test(navigator.userAgent) && window.screen && (window.screen.height === 812 && window.screen.width === 375);

這在之前是沒問題的,新的 iPhone X Series 設(shè)備發(fā)布之后,這個就會兼容就有問題。

iPhone X Series 參數(shù)

機(jī)型 倍率 分辨率 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 就無能為力了。

解決方法

對每個機(jī)型進(jìn)行判斷

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;
}

統(tǒng)一處理方法

因為現(xiàn)在 iPhone 在 iPhone X 之后的機(jī)型都需要適配,所以可以對 X 以后的機(jī)型統(tǒng)一處理,我們可以認(rèn)為這系列手機(jī)的特征是 ios + 長臉。

在 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 判斷,否則可能會誤判一些安卓機(jī)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

苍南县| 田林县| 延庆县| 蒙城县| 玉田县| 文成县| 东港市| 洞口县| 中山市| 仁寿县| 儋州市| 嘉祥县| 连云港市| 云霄县| 罗山县| 泰安市| 乾安县| 望都县| 平凉市| 类乌齐县| 梁河县| 永春县| 汾西县| 富民县| 遂平县| 涞源县| 海口市| 繁峙县| 桂阳县| 九龙城区| 富蕴县| 新野县| 灵台县| 交口县| 新宁县| 绥德县| 肥城市| 丽江市| 城市| 淮南市| 弥勒县|