我正在使用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;
然后要检测它是否是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>