Emulate iPhone X on Chrome

╄→гoц情女王★ 提交于 2019-12-03 16:05:56

According to the iPhone X Human Interface Guidelines, you should enter:

  • 375 for the width
  • 812 for the height
  • 3 for the device pixel ratio

Because the scale factor is 3, you should divide the physical resolution (1125px × 2436px) by 3 to get the logical resolution.

For the user agent string, check out this answer.

To calculate Device Pixel Resolution, use the PPI value which is 458ppi according to the link provided.

According to this answer,

458/150 = 3 DPR

To calculate height and width,

use the given physical resolution: 2436x1125-pixel resolution.

  • 2436/3 = 812 (Height)
  • 1125/3 = 375 (Width)

This is the logical pixel resolution.

For more info:https://stackoverflow.com/a/21413366/4826457

First image should be in img directory saved as iphonex.png or change the code starting with img.src Second image is the screenshot of the result.

Javascript function will dynamically add the iphone x notch. iPhoneX(); on the first line should run after DOM content loaded.

iPhoneX();
window.onresize = window.onorientationchange = function() {
    //Your other functions
    setTimeout(iPhoneX,100);
}

function iPhoneX() {
    if (window.innerWidth == 375 && window.innerHeight == 812) {
        if (!document.getElementById('iphone_layout')) {
            var img = document.createElement('img');
            img.id = 'iphone_layout';
            img.style.position = 'fixed';
            img.style.zIndex = 9999;
            img.style.pointerEvents = 'none';
            img.src = 'img/iphonex.png'
            document.body.insertBefore(img,document.body.children[0]);
        }
    } else if (document.getElementById('iphone_layout')) {
        document.body.removeChild(document.getElementById('iphone_layout'));
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!