【发布时间】:2020-02-19 16:11:59
【问题描述】:
我正在使用window.screen.height 和window.screen.width。
但它没有为我提供正确的平板电脑分辨率。 请帮帮我。
【问题讨论】:
标签: javascript jquery jquery-mobile
我正在使用window.screen.height 和window.screen.width。
但它没有为我提供正确的平板电脑分辨率。 请帮帮我。
【问题讨论】:
标签: javascript jquery jquery-mobile
尝试使用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>
【讨论】:
if 声明,比如if(width < 720 && height < 1080),你在找什么?