是否有可能获得平板电脑的分辨率

问题描述 投票:0回答:1

我正在使用window.screen.heightwindow.screen.width

但是它不能为我提供平板电脑的正确分辨率。请帮助我。

javascript jquery jquery-mobile
1个回答
0
投票

尝试在window.devicePixelRatio中使用屏幕尺寸/分辨率:

var ratio = window.devicePixelRatio || 1;
var width = window.screen.width * ratio;
var height = window.screen.height * ratio;

然后要检测它是否是Asus Nexus 7,您必须使用外部API或扩展名,例如ua-parser.js,可从GitHub的https://github.com/faisalman/ua-parser-js获得。这是我使用他们的库想到的:

var parser = new UAParser();
var result = parser.getResult();
if (result.device.vendor == "Asus" && result.device.model == "Nexus 7" && result.device.type == "tablet")
    // Code to redirect to mobile version website

结合两者:

<script src="ua-parser.min.js"></script>
<script>
   var ratio = window.devicePixelRatio || 1;
   var width = window.screen.width * ratio;
   var height = window.screen.height * ratio;
   var parser = new UAParser();
   var result = parser.getResult();
   if ((width < 700 && height < 900) || (result.device.vendor == "Asus" && result.device.model == "Nexus 7" && result.device.type == "tablet"))
      // Code to redirect to mobile version website
</script>
© www.soinside.com 2019 - 2024. All rights reserved.