接受的答案是正确的,但是如果您使用的是 jQuery,并且 jQuery 需要准备好。在慢速连接上,您会遇到该代码的问题,因此您可能会遇到'$' undefined
错误。
这就是为什么在这种情况下我更喜欢纯 JavaScript:
<meta name="viewport" id="viewport" content="width=device-width; initial-scale=1">
<script type="text/javascript">
(function setViewPort() {
if (screen.width < 640 && (window.orientation == 0 || window.orientation == 180)) {
document.getElementById("viewport").setAttribute("content", "width=480, initial-scale=0.68");
//alert('480 @ 0.68, landscape');
} else if (screen.width < 640) {
// document.getElementById("viewport").setAttribute("content", "width=device-width, initial-scale=1");
// disabled, since its the same as the default. if this one is different, uncomment line above
//alert('device-width @ 1, landscape');
} else if (screen.width >= 640) {
document.getElementById("viewport").setAttribute("content", "width=640; initial-scale=0.5");
//alert('640, high res phones');
}
})();
</script>
在我的情况下,我将纵向设置为 480 像素,缩放为 0.68,横向设置为 320 像素,以便用户可以看到更大的文本(因为这是一个仅限移动设备的页面),如果 screen.width 高于 640(比如在我的 android 5 上)屏幕尺寸为 1900x1080 的手机)到 640 像素,缩放为 0.5(因为宽度始终保持在 320 像素)。
问候, 马克斯