JS监听横竖屏切换

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 06:52   11   0
(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);
})();


注:90,-90是横屏,0是竖屏,屏幕旋转会触发下面的函数
 window.addEventListener('orientationchange',function(){
alert(window.orientation); //这里可以根据orientation做相应的处理
},false);


转自:http://www.jb51.net/article/85438.htm
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP