我正在使用window.screen.height
和window.screen.width
。
但它没有为我提供平板电脑的正确分辨率。 请帮助我。
尝试使用您的屏幕尺寸/分辨率window.devicePixelRatio
:
var ratio = window.devicePixelRatio || 1;
var width = window.screen.width * ratio;
var height = window.screen.height * ratio;
然后要检测它是否是华硕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>