国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

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

2019-11-19 12:12:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

寫在前面

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

現(xiàn)狀

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

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

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

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

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 只能識(shí)別出 iPhone X 和 iPhone XS,對(duì)于 iPhone XS Max 和 iPhone XR 就無(wú)能為力了。

解決方法

對(duì)每個(gè)機(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)一處理方法

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

在 H5 上可以簡(jiǎn)單處理。

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) {}

媒體查詢無(wú)法識(shí)別是不是 iOS,還得加一層 JS 判斷,否則可能會(huì)誤判一些安卓機(jī)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沙洋县| 连州市| 阳信县| 时尚| 南漳县| 毕节市| 凭祥市| 辰溪县| 保定市| 临颍县| 资兴市| 若羌县| 阜康市| 应城市| 泽普县| 三河市| 白水县| 玛多县| 来宾市| 长子县| 喜德县| 连城县| 泸州市| 翼城县| 修文县| 侯马市| 沛县| 曲阜市| 科技| 肇州县| 平舆县| 灵璧县| 固始县| 宜城市| 邯郸县| 大姚县| 江阴市| 保康县| 措美县| 汉阴县| 南投市|