2012-09-17 60 views
0

我需要知道手機或Pad上的用戶是否正在查看Verticaly或Horizo​​ntaly網站來切換圖像比例。檢測哪個視圖在手機上處於活動狀態

例子:

如果用戶在水平方向查看的頁面,我的形象的風格,看看它的滿量程將

img{width:100%; height:auto;} 

但如果它是垂直的CSS將需要切換到

img{width:auto; height:100%;} 

那麼,如何可以實現的jQuery或Javascript或媒體查詢是這樣的......等等,所以我可以根據其查看用戶以良好的作風是觀衆ŧ他在移動或iPad上翻頁?

感謝

回答

2

您可以檢測方位像一些javascript:

detectOrientation(); 
    window.onorientationchange = detectOrientation; 
    function detectOrientation(){ 
     if(typeof window.onorientationchange != 'undefined'){ 
      if (orientation == 0) { 
       //Do Something In Portrait Mode 
      } 
      else if (orientation == 90) { 
       //Do Something In Landscape Mode 
      } 
      else if (orientation == -90) { 
       //Do Something In Landscape Mode 
      } 
      else if (orientation == 180) { 
       //Do Something In Landscape Mode 
      } 
     } 
    } 

http://www.devinrolsen.com/javascript-mobile-orientation-detection/

+0

似乎在iPad,iPhone,但Android上工作的偉大...它做了一些難聽的話:( – Warface

+0

@Warface它應該是跨平臺的,檢查出的http: //davidbcalhoun.com/2010/dealing-with-device-orientation獲取有關該主題的最完整信息,包括回退! – JKirchartz

0

不容抓獲你只需要使用一定的斷點響應CSS?

有點像這樣:

@media screen and (max-width: 123px /* your value */) { 
    img{width:auto; height:100%;} 
} 
相關問題