我需要页面的视口宽度为横向 950 像素,纵向为 630 像素。不幸的是,这些像素宽度是不灵活的。
我正在使用 CSS 媒体查询根据方向调整 DOM 内容的宽度。
我正在侦听 JS 中的方向更改事件,以在方向更改时调整视口的大小。
初始页面加载(纵向或横向)时一切看起来都不错。
但是,在方向更改后,Mobile Safari 会保留以前的比例设置,而不是使视口适合屏幕。双击或两次将其理顺。但我需要它是自动的。
我附上了下面的源代码,并将其上传到演示URL。
以下是方向更改后的屏幕截图:从横向到纵向,从纵向到横向。
如何强制 Safari 自动将视口宽度设置为屏幕宽度?还是我对这一切都错了?
更改视口中的各种比例设置没有帮助。设置maximum-scale=1.0
orinitial-scale=1.0
似乎会覆盖我的width
, 设置width
为device-width
(即我的 iPad 2 上的 1024 或 768),留下死边距空间。设置minimum-scale=1.0
似乎什么都不做。
<!DOCTYPE html>
<html>
<head>
<title>iPad orientation</title>
<meta name="viewport" content="user-scalable=yes">
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">;
/**
* update viewport width on orientation change
*/
function adapt_to_orientation() {
// determine new screen_width
var screen_width;
if (window.orientation == 0 || window.orientation == 180) {
// portrait
screen_width = 630;
} else if (window.orientation == 90 || window.orientation == -90) {
// landscape
screen_width = 950;
}
// resize meta viewport
$('meta[name=viewport]').attr('content', 'width='+screen_width);
}
$(document).ready(function() {
// bind to handler
$('body').bind('orientationchange', adapt_to_orientation);
// call now
adapt_to_orientation();
});
</script>
<style type="text/css" media="screen">
body {
margin: 0;
}
#block {
background-color: #333;
color: #fff;
font-size: 128px;
/* landscape */
width: 950px;
}
#block .right {
float: right
}
@media only screen and (orientation:portrait) {
#block {
width: 630px;
}
}
</style>
</head>
<body>
<div id="block">
<div class="right">→</div>
<div class="left">←</div>
</div>
</body>
</html>