JS监听横竖屏切换

(function(){

var init = function(){

var updateOrientation = function(){//方向改变执行的函数

var orientation = window.orientation;

switch(orientation){

case 90:

case -90:

orientation = 'landscape'; //这里是横屏

break;

default:

orientation = 'portrait'; //这里是竖屏

break;

}

//html根据不同的旋转状态,加上不同的class,横屏加上landscape,竖屏

//加上portrait

document.body.parentNode.setAttribute('class',orientation);

};

// 每次旋转,调用这个事件。

window.addEventListener('orientationchange',updateOrientation,false);

// 事件的初始化

updateOrientation();

};

window.addEventListener('DOMContentLoaded',init,false);

})();
  1. 注:90,-90是横屏,0是竖屏,屏幕旋转会触发下面的函数

window.addEventListener('orientationchange',function(){

 alert(window.orientation); //这里可以根据orientation做相应的处理

},false);



转自:http://www.jb51.net/article/85438.htm

你可能感兴趣的:(h5,js)