摘要:上週做的小項目要接入我們的 App,但需要橫豎屏使用不同樣式——橫屏一套、豎屏一套。本以為調用手機 App 提供的 API 就能解決,結果 iOS 和安卓返回的結果並不一致,各種頭疼。於是改用 CSS3 的 media 查詢,根據屏幕尺寸顯示不同樣式,可問題依然存在。
iOS 上旋轉屏幕時一切正常,但安卓機卻沒反應,橫屏顯示的仍是豎屏樣式。
查資料後發現,CSS3 的 media 若要在移動端有良好效果,需要在頁頭加上這段代碼:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
但這段代碼我卻不能用——因為我的頁面做了適配,會根據屏幕大小自適應地縮放字號和元素尺寸。一旦加上這段 meta,適配就失效了,所以只能另尋他法。
移動端設備提供了一個事件:orientationChange 事件。它由蘋果公司為 Safari 添加,目的是讓開發者能夠判斷用戶何時在橫向與縱向視圖之間切換。
設備旋轉時會觸發該事件:
// Listen for orientation changes window.addEventListener("orientationchange", function() { // Announce the new orientation number alert(window.orientation); }, false);
只要用戶改變了設備的查看模式,就會觸發 orientationChange 事件。此時 event 對象本身並不包含有價值的信息,唯一相關的資訊可以通過 window.orientation 取得。
orientation 屬性有三個值:0、90、-90。0 為豎屏(portrait),-90 表示設備向右旋轉的橫屏(landscape),90 表示向左旋轉的橫屏(landscape)。此外還有 180,代表翻轉過來的倒置豎屏,但這種模式至今未獲支持。
如圖所示:

因此,結合 orientationChange 事件與 window.orientation 屬性,我們就能比較好地判斷設備處於橫屏還是豎屏:
(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); })();
這樣就能根據旋轉狀態給 html 加上對應 class,CSS 就可以這樣寫:
/**豎屏 body顯示紅色**/ .portrait body div{ background: red; } /**橫屏 body顯示藍色**/ .landscape body div{ background: blue; }
另外一種寫法是借助 media queries:
@media all and (orientation: portrait) { body div {background: red;} } @media all and (orientation: landscape) { body div {background: blue; } }
這種 orientation media query 在 iOS 3.2+、安卓 2.0+ 及其他瀏覽器上有效,代碼更簡潔,屬於純 CSS 方案,設備旋轉時會自動套用對應方向的樣式。
有些設備既不提供 orientationchange 事件,也不觸發窗口 resize 事件,還不支持 media queries,這時該怎麼辦?可以用 resize 事件配合 innerWidth、innerHeight 來判斷:寬小於高為豎屏,寬大於高為橫屏。
(function(){ var updateOrientation = function(){ var orientation = (window.innerWidth > window.innerHeight) ? 'landscape' : 'portrait'; document.body.parentNode.setAttribute('class',orientation); }; var init = function(){ updateOrientation(); //監聽resize事件 window.addEventListener('resize',updateOrientation,false); }; window.addEventListener('DOMContentLoaded',init,false); })();
(function(){ var supportOrientation = (typeof window.orientation === 'number' && typeof window.onorientationchange === 'object'); var init = function(){ var htmlNode = document.body.parentNode, orientation; var updateOrientation = function(){ if(supportOrientation){ orientation = window.orientation; switch(orientation){ case 90: case -90: orientation = 'landscape'; break; default: orientation = 'portrait'; break; } }else{ orientation = (window.innerWidth > window.innerHeight) ? 'landscape' : 'portrait'; } htmlNode.setAttribute('class',orientation); }; if(supportOrientation){ window.addEventListener('orientationchange',updateOrientation,false); }else{ //監聽resize事件 window.addEventListener('resize',updateOrientation,false); } updateOrientation(); }; window.addEventListener('DOMContentLoaded',init,false); })();
利用這個方法,就能搞定移動端設備橫豎屏檢測這個煩人的問題了。如有錯誤之處,歡迎指正。