我有一个宽度为590像素的移动页面。因此,我设置了以下视口:
<meta name = "viewport" content = "width = 590">
当我第一次以纵向或横向方式访问页面时,它看起来很好。页面的宽度完全填充。但是当我更改方向时,视口没有改变。当我从纵向变为横向时,视口比590px更宽,反之亦然。
仅在Galaxy S2上进行了测试。
// All CSS that is common to both
@media all and (min-device-width:601px)and (orientation:landscape){
// All CSS for Landscape and Desktop
}
@media only screen and (max-device-width:600px)and (orientation:portrait){
// All CSS for Portrait view
}
<meta name="viewport" id="viewport" content="width=480, initial-scale=0.25, maximum-scale=1.0;" />
最后我不得不使用一些Javascript来重新编写视口设置,当页面检测到手机的旋转时(感谢Vinayak.B 原始文章)。
//Code to display on mobiles
//========================================
var swidth = window.screen.width;
//These were the values of my website CSS container for portrait and landscape
var vpwidth = 480;
var vlwidth = 960;
updateOrientation();
window.addEventListener('orientationchange', updateOrientation, false);
function updateOrientation() {
var viewport = document.querySelector("meta[name=viewport]");
switch (window.orientation) {
case 0: //portrait
//set the viewport attributes to whatever you want!
viewport.setAttribute('content', 'width=' + vpwidth + ', initial-scale=0.25, maximum-scale=1.0;')
break;
case 90: case -90: //landscape
//set the viewport attributes to whatever you want!
viewport.setAttribute('content', 'width=' + vlwidth + ', initial-scale=0.25, maximum-scale=1.0;')
break;
default:
//set the viewport attributes to whatever you want!
viewport.setAttribute('content', 'width=' + vpwidth + ', initial-scale=0.25, maximum-scale=1.0;')
break;
}
//alert(swidth + ' lead to an initial width of ' + vpwidth + ' and a rotate width of ' + vlwidth);
}
<meta name = "viewport" content = "width=device-width">
这个可以处理方向变化。
我曾经遇到过同样的问题,这是我的解决方案。
在屏幕方向改变后重新加载页面,然后根据新的方向设置视口变量。
window.onorientationchange = function() {
/*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the
left, or landscape mode with the screen turned to the right. */
var orientation = window.orientation;
switch(orientation) {
case 0:
var current = $('body').attr('class');
if(current != 'ps-active'){
location.reload();
}
break;
case 90:
var current = $('body').attr('class');
if(current != 'ps-active'){
location.reload();
}
break;
case -90:
var current = $('body').attr('class');
if(current != 'ps-active'){
location.reload();
}
break;
}
}
$(document).ready(function() {
window.onload = function() {
/*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the
left, or landscape mode with the screen turned to the right. */
var orientation = window.orientation;
switch(orientation) {
case 0:
viewport = document.querySelector("meta[name=viewport]");
viewport.setAttribute('content', 'width=device-width; initial-scale=0.2; maximum-scale=1.0; user-scalable=yes;');
$('.container').show();
break;
case 90:
viewport = document.querySelector("meta[name=viewport]");
viewport.setAttribute('content', 'width=device-width; initial-scale=0.4; maximum-scale=1.0; user-scalable=yes;');
$('.container').show();
break;
case -90:
viewport = document.querySelector("meta[name=viewport]");
viewport.setAttribute('content', 'width=device-width; initial-scale=0.4; maximum-scale=1.0; user-scalable=yes;');
$('.container').show();
break;
default:
$('.container').show();
break;
}
}
以下是我使用的方法(在iOS Safari和Chrome中测试通过)。
我想要在纵向模式下将视口强制设为400像素,在横向模式下将其设为600像素。
var resize = function() {
$('body').removeClass('landscape').removeClass('portrait').addClass(orientation).css('width', $(window).width() + 'px');
}
var orientation = null;
var onOrientationChange = function () {
switch(window.orientation) {
case -90:
case 90:
orientation = 'landscape';
break;
default:
orientation = 'portrait';
break;
}
if(screen.width < 768) {
if(orientation == 'landscape') {
var scale = Math.round(screen.height / 600 * 10) / 10;
$('#meta-viewport').attr('content', 'width=600px, initial-scale='+scale+', maximum-scale='+scale+', minimum-scale='+scale+', user-scalable=no'); // landscape mobile
} else {
var scale = Math.round(screen.width / 400 * 10) / 10;
$('#meta-viewport').attr('content', 'width=400px, initial-scale='+scale+', maximum-scale='+scale+', minimum-scale='+scale+', user-scalable=no'); // portrait mobile
}
} else if(screen.width >= 768 && screen.width < 1200) {
var scale = Math.round(screen.width / 960 * 10) / 10;
$('#meta-viewport').attr('content', 'width=960px, initial-scale='+scale+', maximum-scale='+scale+', minimum-scale='+scale+', user-scalable=no');
} else if(screen.width >= 1200) {
$('#meta-viewport').attr('content', 'width=device-width, user-scalable=yes');
}
resize();
}
$(document).ready(function() {
$(window).resize(resize);
$(window).bind('orientationchange', onOrientationChange);
onOrientationChange();
});
在方向改变后重置视口。只要您拥有此JS,它就可以工作。
<meta name="viewport/>
var maxWidth = screen.width;
var viewport = document.getElementsByName('viewport')[0];
viewport.setAttribute('content', 'width = ' + maxWidth + ', minimum-scale=1.0, maximum-scale=1.0, user-scalable=no');
在我的测试中,matchMedia函数在Android上表现非常出色:
var mql = window.matchMedia("(orientation: landscape)");
if (mql.matches) {
/* The device is currently in landscape orientation */
}
else {
/* The device is currently in portrait orientation */
}
我非常确定只能使用JS在Galaxy中检测到方向变化。 尝试下面的代码片段。
来自相关帖子: 使用JavaScript在Android中进行屏幕方向更改
function orientation_changed ()
{
if ( is_portrait() )
{
//do something
}
else if ( is_landscape() )
{
// do something else
}
clearTimeout(window.t);
delete window.t;
}
window.t = undefined;
window.onorientationchange = function (event)
{
window.t = setTimeout('orientation_changed();', 250);
}
function is_landscape()
{
var uagent = navigator.userAgent.toLowerCase();
if ( uagent.search('ipad') > -1 )
{
var r = ( window.orientation == 90 || window.orientation == -90 );
}
else
{
var r = ( screen.width > screen.height );
}
return r;
}