html旋轉螢幕事件監聽

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   io   ar   

近期做服務號開發,在做圖片展示的時候需要橫豎屏的檢測實現圖片大小不同的展示。

添加旋轉螢幕事件偵聽,可隨時發現旋轉螢幕狀態(左旋、右旋還是沒旋)。

摘自:http://bbs.phonegap100.com/thread-28-1-1.html

//js 判斷螢幕是否旋轉

4. 旋轉螢幕事件:onorientationchange添加旋轉螢幕事件偵聽,可隨時發現旋轉螢幕狀態(左旋、右旋還是沒旋)。例子:// 判斷螢幕是否旋轉function orientationChange() {    switch(window.orientation) {      case 0:             alert("肖像模式 0,screen-width: " + screen.width + "; screen-height:" + screen.height);            break;      case -90:             alert("左旋 -90,screen-width: " + screen.width + "; screen-height:" + screen.height);            break;      case 90:               alert("右旋 90,screen-width: " + screen.width + "; screen-height:" + screen.height);            break;      case 180:             alert("風景模式 180,screen-width: " + screen.width + "; screen-height:" + screen.height);          break;    };<br>};// 添加事件監聽addEventListener(‘load‘, function(){    orientationChange();    window.onorientationchange = orientationChange;});

我們在開發Mobile Web應用時,一個最佳實務就是採用流式布局,保證最大可能地利用有限的螢幕空間。

window.orientation屬性與onorientationchange事件    window.orientation :這個屬性給出了當前裝置的螢幕方向,0表示豎屏,正負90表示橫屏(向左與向右)模式onorientationchange : 在每次螢幕方向在橫豎屏間切換後,就會觸發這個window事件,用法與傳統的事件類別似

1、使用onorientationchange事件的回呼函數,來動態地為body標籤添加一個叫orient的屬性,同時以body[orient=landspace]或body[orient=portrait]的方式在css中定義對應的樣式,這樣就可以實現在不同的螢幕模式下顯示不同的樣式。如下程式碼範例:

<!Doctype html>      <html>          <head>              <meta charset="utf-8">              <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">              <title>橫豎屏切換檢測</title>              <style type="text/css">                  body[orient=landscape]{                      background-color: #ff0000;                  }                        body[orient=portrait]{                      background-color: #00ffff;                  }              </style>          </head>          <body orient="landspace">              <div>                  內容              </div>              <script type="text/javascript">                  (function(){                      if(window.orient==0){                          document.body.setAttribute("orient","portrait");                      }else{                          document.body.setAttribute("orient","landscape");                      }                  })();                  window.onorientationchange=function(){                      var body=document.body;                      var viewport=document.getElementById("viewport");                      if(body.getAttribute("orient")=="landscape"){                          body.setAttribute("orient","portrait");                      }else{                          body.setAttribute("orient","landscape");                      }                  };              </script>          </body>      </html>  

2、類似的思路,不通過CSS的屬性選取器來實現,如下代碼的實現方案:

<!Doctype html>      <html>          <head>              <meta charset="utf-8">              <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">              <title>橫豎屏切換檢測</title>              <style type="text/css">                  .landscape body {                      background-color: #ff0000;                  }                        .portrait body {                      background-color: #00ffff;                  }              </style>          </head>          <body orient="landspace">              <div>                  內容              </div>              <script type="text/javascript">                  (function(){                      var init=function(){                          var updateOrientation=function(){                              var orientation=window.orientation;                              switch(orientation){                                  case 90:                                   case -90:                                      orientation="landscape";                                      break;                                  default:                                      orientation="portrait";                                      break;                              }                              document.body.parentNode.setAttribute("class",orientation);                          };                                window.addEventListener("orientationchange",updateOrientation,false);                          updateOrientation();                      };                      window.addEventListener("DOMContentLoaded",init,false);                  })();              </script>          </body>      </html>  

使用media query方式

    這是一種更為方便的方式,使用純CSS就實現了對應的功能,如下代碼示範:

<!Doctype html>      <html>          <head>              <meta charset="utf-8">              <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">              <title>橫豎屏切換檢測</title>              <style type="text/css">                  @media all and (orientation : landscape) {                      body {                           background-color: #ff0000;                       }                  }                        @media all and (orientation : portrait){                      body {                          background-color: #00ff00;                      }                  }              </style>          </head>          <body>              <div>                  內容              </div>          </body>      </html>  

低版本瀏覽器的平穩降級

     如果目標行動瀏覽器不支援media query,同時window.orientation也不存在,則我們需要採用另外一種方式來實現————使用定時器“偽即時”地對比當前視窗的高(window.innerHeight)與寬(window.innerWidth)之比,從而判定當前的橫豎屏狀態。如下代碼所示:

<!Doctype html>      <html>          <head>              <meta charset="utf-8">              <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">              <title>按鍵</title>              <style type="text/css">                  .landscape body {                      background-color: #ff0000;                  }                        .portrait body {                      background-color: #00ffff;                  }              </style>              <script type="text/javascript">                  (function(){                      var updateOrientation=function(){                          var orientation=(window.innerWidth > window.innerHeight)? "landscape" : "portrait";                          document.body.parentNode.setAttribute("class",orientation);                      };                            var init=function(){                          updateOrientation();                          window.setInterval(updateOrientation,5000);                      };                      window.addEventListener("DOMContentLoaded",init,false);                  })();              </script>          </head>          <body>              <div>                  內容              </div>          </body>      </html>  

統一解決方案

將以上的兩種解決方案整合在一起,就可以實現一個跨瀏覽器的解決方案,如下代碼:

    <!Doctype html>      <html>          <head>              <meta charset="utf-8">              <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">              <title>橫豎屏切換檢測</title>              <style type="text/css">                  .landscape body {                      background-color: #ff0000;                  }                        .portrait body {                      background-color: #00ffff;                  }              </style>              <script type="text/javascript">                  (function(){                      var supportOrientation=(typeof window.orientation == "number" && typeof window.onorientationchange == "object");                            var updateOrientation=function(){                          if(supportOrientation){                              updateOrientation=function(){                                  var orientation=window.orientation;                                  switch(orientation){                                      case 90:                                      case -90:                                          orientation="landscape";                                          break;                                      default:                                          orientation="portrait";                                  }                                  document.body.parentNode.setAttribute("class",orientation);                              };                          }else{                              updateOrientation=function(){                                  var orientation=(window.innerWidth > window.innerHeight)? "landscape":"portrait";                                  document.body.parentNode.setAttribute("class",orientation);                              };                          }                          updateOrientation();                      };                            var init=function(){                          updateOrientation();                          if(supportOrientation){                              window.addEventListener("orientationchange",updateOrientation,false);                          }else{                                  window.setInterval(updateOrientation,5000);                          }                      };                      window.addEventListener("DOMContentLoaded",init,false);                  })();              </script>          </head>          <body>              <div>                  內容              </div>          </body>      </html>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.