DIV CSS 佈局教程網

H5 橫豎屏檢測方案比較
編輯:關於JavaScript     

H5 橫豎屏檢測

行動版網頁常需偵測橫豎屏切換。orientationchange 在部分瀏覽器相容性不佳,可搭配 resize 或 CSS media query。

方案一:orientationchange

window.addEventListener("orientationchange", function(event){
  // 依 orientation 判斷
}, false);

orientationchange 的相容性如下:

screen.orientation 的相容性如下:

方案二:resize

var orientation = window.innerWidth > window.innerHeight ? "landscape" : "portrait";

方案三:CSS media query

可動態注入 CSS,透過 font-family 是否命中 portrait 或 landscape 來判斷狀態。

portrait 效果:

landscape 效果:

最終可封裝為 orientationchange-fix,在支援時使用原生事件,不支援時使用 resize 或樣式偵測。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved