html5屏幕旋转事件 onorientationchange

html5屏幕旋转事件 onorientationchange

很多用html5开发移动应用的童鞋不知道怎么实现监听屏幕旋转,下面为大家介绍 html5如何实现屏幕旋转

添加屏幕旋转事件侦听,可随时发现屏幕旋转状态(左旋、右旋还是没旋)。
例子:

// 判断屏幕是否旋转
  1. 4. 屏幕旋转事件:onorientationchange

  2. 添加屏幕旋转事件侦听,可随时发现屏幕旋转状态(左旋、右旋还是没旋)。例子:

  3. // 判断屏幕是否旋转

  4. function orientationChange() {

  5.     switch(window.orientation) {

  6.       case 0: 

  7.             alert("肖像模式 0,screen-width: " + screen.width + "; screen-height:" + screen.height);

  8.             break;

  9.       case -90: 

  10.             alert("左旋 -90,screen-width: " + screen.width + "; screen-height:" + screen.height);

  11.             break;

  12.       case 90:   

  13.             alert("右旋 90,screen-width: " + screen.width + "; screen-height:" + screen.height);

  14.             break;

  15.       case 180:   

  16.           alert("风景模式 180,screen-width: " + screen.width + "; screen-height:" + screen.height);

  17.           break;

  18.     };<br>};

  19. // 添加事件监听

  20. addEventListener('load', function(){

  21.     orientationChange();

  22.     window.onorientationchange = orientationChange;

  23. });


我们在开发Mobile Web应用时,一个最佳实践就是采用流式布局,保证最大可能地利用有限的屏幕空间。
  1. window.orientation属性与onorientationchange事件    

  2. window.orientation :这个属性给出了当前设备的屏幕方向,0表示竖屏,正负90表示横屏(向左与向右)模式

  3. onorientationchange : 在每次屏幕方向在横竖屏间切换后,就会触发这个window事件,用法与传统的事件类似
1:使用onorientationchange事件的回调函数,来动态地为body标签添加一个叫orient的属性,同时以body[orient=landspace]或body[orient=portrait]的方式在css中定义对应的样式,这样就可以实现在不同的屏幕模式下显示不同的样式。如下代码示例:
Html代码
  1. <!Doctype html>  
  2.     <html>  
  3.         <head>  
  4.             <meta charset="utf-8">  
  5.             <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">  
  6.             <title>横竖屏切换检测</title>  
  7.             <style type="text/css">  
  8.                 body[orient=landscape]{  
  9.                     background-color: #ff0000;  
  10.                 }  
  11.       
  12.                 body[orient=portrait]{  
  13.                     background-color: #00ffff;  
  14.                 }  
  15.             </style>  
  16.         </head>  
  17.         <body orient="landspace">  
  18.             <div>  
  19.                 内容  
  20.             </div>  
  21.             <script type="text/javascript">  
  22.                 (function(){  
  23.                     if(window.orient==0){  
  24.                         document.body.setAttribute("orient","portrait");  
  25.                     }else{  
  26.                         document.body.setAttribute("orient","landscape");  
  27.                     }  
  28.                 })();  
  29.                 window.onorientatiοnchange=function(){  
  30.                     var body=document.body;  
  31.                     var viewport=document.getElementById("viewport");  
  32.                     if(body.getAttribute("orient")=="landscape"){  
  33.                         body.setAttribute("orient","portrait");  
  34.                     }else{  
  35.                         body.setAttribute("orient","landscape");  
  36.                     }  
  37.                 };  
  38.             </script>  
  39.         </body>  
  40.     </html>  
2: 类似的思路,不通过CSS的属性选择器来实现,如下代码的实现方案:
Html代码
  1. <!Doctype html>  
  2.     <html>  
  3.         <head>  
  4.             <meta charset="utf-8">  
  5.             <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">  
  6.             <title>横竖屏切换检测</title>  
  7.             <style type="text/css">  
  8.                 .landscape body {  
  9.                     background-color: #ff0000;  
  10.                 }  
  11.       
  12.                 .portrait body {  
  13.                     background-color: #00ffff;  
  14.                 }  
  15.             </style>  
  16.         </head>  
  17.         <body orient="landspace">  
  18.             <div>  
  19.                 内容  
  20.             </div>  
  21.             <script type="text/javascript">  
  22.                 (function(){  
  23.                     var init=function(){  
  24.                         var updateOrientation=function(){  
  25.                             var orientation=window.orientation;  
  26.                             switch(orientation){  
  27.                                 case 90:   
  28.                                 case -90:  
  29.                                     orientation="landscape";  
  30.                                     break;  
  31.                                 default:  
  32.                                     orientation="portrait";  
  33.                                     break;  
  34.                             }  
  35.                             document.body.parentNode.setAttribute("class",orientation);  
  36.                         };  
  37.       
  38.                         window.addEventListener("orientationchange",updateOrientation,false);  
  39.                         updateOrientation();  
  40.                     };  
  41.                     window.addEventListener("DOMContentLoaded",init,false);  
  42.                 })();  
  43.             </script>  
  44.         </body>  
  45.     </html>  
使用media query方式

    这是一种更为方便的方式,使用纯CSS就实现了对应的功能,如下代码演示:
Html代码  收藏代码
  1. <!Doctype html>  
  2.     <html>  
  3.         <head>  
  4.             <meta charset="utf-8">  
  5.             <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">  
  6.             <title>横竖屏切换检测</title>  
  7.             <style type="text/css">  
  8.                 @media all and (orientation : landscape) {  
  9.                     body {   
  10.                         background-color: #ff0000;   
  11.                     }  
  12.                 }  
  13.       
  14.                 @media all and (orientation : portrait){  
  15.                     body {  
  16.                         background-color: #00ff00;  
  17.                     }  
  18.                 }  
  19.             </style>  
  20.         </head>  
  21.         <body>  
  22.             <div>  
  23.                 内容  
  24.             </div>  
  25.         </body>  
  26.     </html>  

低版本浏览器的平稳降级

     如果目标移动浏览器不支持media query,同时window.orientation也不存在,则我们需要采用另外一种方式来实现————使用定时器“伪实时”地对比当前窗口的高(window.innerHeight)与宽(window.innerWidth)之比,从而判定当前的横竖屏状态。如下代码所示:
Html代码
  1. <!Doctype html>  
  2.     <html>  
  3.         <head>  
  4.             <meta charset="utf-8">  
  5.             <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">  
  6.             <title>按键</title>  
  7.             <style type="text/css">  
  8.                 .landscape body {  
  9.                     background-color: #ff0000;  
  10.                 }  
  11.       
  12.                 .portrait body {  
  13.                     background-color: #00ffff;  
  14.                 }  
  15.             </style>  
  16.             <script type="text/javascript">  
  17.                 (function(){  
  18.                     var updateOrientation=function(){  
  19.                         var orientation=(window.innerWidth > window.innerHeight)? "landscape" : "portrait";  
  20.                         document.body.parentNode.setAttribute("class",orientation);  
  21.                     };  
  22.       
  23.                     var init=function(){  
  24.                         updateOrientation();  
  25.                         window.setInterval(updateOrientation,5000);  
  26.                     };  
  27.                     window.addEventListener("DOMContentLoaded",init,false);  
  28.                 })();  
  29.             </script>  
  30.         </head>  
  31.         <body>  
  32.             <div>  
  33.                 内容  
  34.             </div>  
  35.         </body>  
  36.     </html>  
统一解决方案

将以上的两种解决方案整合在一起,就可以实现一个跨浏览器的解决方案,如下代码:
Html代码

  1.     <!Doctype html>  
  2.     <html>  
  3.         <head>  
  4.             <meta charset="utf-8">  
  5.             <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">  
  6.             <title>横竖屏切换检测</title>  
  7.             <style type="text/css">  
  8.                 .landscape body {  
  9.                     background-color: #ff0000;  
  10.                 }  
  11.       
  12.                 .portrait body {  
  13.                     background-color: #00ffff;  
  14.                 }  
  15.             </style>  
  16.             <script type="text/javascript">  
  17.                 (function(){  
  18.                     var supportOrientation=(typeof window.orientation == "number" && typeof window.onorientationchange == "object");  
  19.       
  20.                     var updateOrientation=function(){  
  21.                         if(supportOrientation){  
  22.                             updateOrientation=function(){  
  23.                                 var orientation=window.orientation;  
  24.                                 switch(orientation){  
  25.                                     case 90:  
  26.                                     case -90:  
  27.                                         orientation="landscape";  
  28.                                         break;  
  29.                                     default:  
  30.                                         orientation="portrait";  
  31.                                 }  
  32.                                 document.body.parentNode.setAttribute("class",orientation);  
  33.                             };  
  34.                         }else{  
  35.                             updateOrientation=function(){  
  36.                                 var orientation=(window.innerWidth > window.innerHeight)? "landscape":"portrait";  
  37.                                 document.body.parentNode.setAttribute("class",orientation);  
  38.                             };  
  39.                         }  
  40.                         updateOrientation();  
  41.                     };  
  42.       
  43.                     var init=function(){  
  44.                         updateOrientation();  
  45.                         if(supportOrientation){  
  46.                             window.addEventListener("orientationchange",updateOrientation,false);  
  47.                         }else{      
  48.                             window.setInterval(updateOrientation,5000);  
  49.                         }  
  50.                     };  
  51.                     window.addEventListener("DOMContentLoaded",init,false);  
  52.                 })();  
  53.             </script>  
  54.         </head>  
  55.         <body>  
  56.             <div>  
  57.                 内容  
  58.             </div>  
  59.         </body>  
  60.     </html>  


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值