2011-08-17 58 views

回答

3

你可以检查视口的宽度和高度,看看哪一个是较大的:

var isLandscapeMode = window.innerWidth > window.innerHeight; 

if (isLandscapeMode) { 
    // fit page to wide orientation here... 
} 

有关移动设备的视口尺寸的更深入的讨论,请参阅 http://www.quirksmode.org/mobile/viewports2.html

(更新代码)

+0

+1好看又简单:) – walialu 2011-08-17 13:18:05

1

您可以这样做...

var currOrientation= ''; 
/************************ 
* Changes the (obj) element's class, to 
* -vrt (vertical) 
* -hrz (horizontal) 
* depending on its width 
*************************/ 
var orientation = function(obj){ 
    var w = getWidth(), 
     h = getHeight(); 

    if(w <= h && currOrientation !== 'vrt'){ 
     obj.className = 'vrt'; 
     currOrientation = 'vrt'; 
    } 
    else if(currOrientation !== 'hrz') { 
     obj.className = 'hrz'; 
     currOrientation = 'hrz'; 
    } 
} 
/************************ 
* Binding a repeating timer 
************************/ 
var orientationINIT = function() { 
    var content; 
    if((content = document.getElementsByTagName('body')[0]) != undefined){ 
     orientation(content); 

     var Orient = setInterval(function() { 
      orientation(content); 
     }, 500); //each 500ms a call for orientation change 
    } 
} 

orientationINIT应该被称为上页面加载或DOM完成(或在页面的底部)

其中的getHeight()和的getWidth()应该是在数返回窗口的宽度/高度简单的功能形成。