通过分辨率区分iPhone型号(更新至13系列)

原理

通过获取设备逻辑像素宽高(window.screen.widthwindow.screen.height)与DPR(window.devicePixelRatio)这三要素然后依据如下图进行判断(数据已查阅多方资料核对)。
在这里插入图片描述

核心代码

          function iPhoneModel() {
            var isIphone = /iphone/gi.test(navigator.userAgent);
            if (isIphone) {
                var dpr = window.devicePixelRatio,
                    screenWidth = window.screen.width,
                    screenHeight = window.screen.height,
                    modelList = {
                        '320*480*1': '2G/3G/3GS',
                        '320*480*2': '4/4S',
                        '320*568*2': '5/5S/5C/SE',
                        '375*667*2': '6/6S/7/8/SE2',
                        '414*736*3': '6Plus/6S Plus/7Plus/8Plus',
                        '375*812*3': 'X/XS/11 Pro',
                        '414*896*2': '11/XR',
                        '414*896*3': 'XS_Max/11_Pro_Max',
                        '360*780*3': '12_Mini/13_Mini',
                        '390*844*3': '12/12_Pro/13/13_Pro',
                        '428*926*3': '12_Pro_Max/13_Pro_Max'
                    }

                return modelList[screenWidth + '*' + screenHeight + '*' + dpr] || 'iPhone';
            } else {
                return false;
            }
        }

Demo

<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>判断iPhone型号</title>
</head>

<body>
    <script>
        function iPhoneModel() {
            var isIphone = /iphone/gi.test(navigator.userAgent);
            if (isIphone) {
                var dpr = window.devicePixelRatio,
                    screenWidth = window.screen.width,
                    screenHeight = window.screen.height,
                    modelList = {
                        '320*480*1': '2G/3G/3GS',
                        '320*480*2': '4/4S',
                        '320*568*2': '5/5S/5C/SE',
                        '375*667*2': '6/6S/7/8/SE2',
                        '414*736*3': '6Plus/6S Plus/7Plus/8Plus',
                        '375*812*3': 'X/XS/11 Pro',
                        '414*896*2': '11/XR',
                        '414*896*3': 'XS_Max/11_Pro_Max',
                        '360*780*3': '12_Mini/13_Mini',
                        '390*844*3': '12/12_Pro/13/13_Pro',
                        '428*926*3': '12_Pro_Max/13_Pro_Max'
                    }

                return modelList[screenWidth + '*' + screenHeight + '*' + dpr] || 'iPhone';
            } else {
                return false;
            }
        }
        document.write(iPhoneModel())
    </script>
</body>

</html>

我的个人公众号:归园田居 每日分享前端知识与资讯~
请添加图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值