3 回答

TA貢獻1806條經(jīng)驗 獲得超5個贊
可靠的方法是同時聽調(diào)整大小和方向變化的事件
var previousOrientation = window.orientation;var checkOrientation = function(){ if(window.orientation !== previousOrientation){ previousOrientation = window.orientation; // orientation changed, do your magic here }};window.addEventListener("resize", checkOrientation, false);window.addEventListener("orientationchange", checkOrientation, false); // (optional) Android doesn't always fire orientationChange on 180 degree turnssetInterval(checkOrientation, 2000);
|==============================================================================| | Device | Events Fired | orientation | innerWidth | screen.width | |==============================================================================| | iPad 2 | resize | 0 | 1024 | 768 | | (to landscape) | orientationchange | 90 | 1024 | 768 | |----------------+-------------------+-------------+------------+--------------| | iPad 2 | resize | 90 | 768 | 768 | | (to portrait) | orientationchange | 0 | 768 | 768 | |----------------+-------------------+-------------+------------+--------------| | iPhone 4 | resize | 0 | 480 | 320 | | (to landscape) | orientationchange | 90 | 480 | 320 | |----------------+-------------------+-------------+------------+--------------| | iPhone 4 | resize | 90 | 320 | 320 | | (to portrait) | orientationchange | 0 | 320 | 320 | |----------------+-------------------+-------------+------------+--------------| | Droid phone | orientationchange | 90 | 320 | 320 | | (to landscape) | resize | 90 | 569 | 569 | |----------------+-------------------+-------------+------------+--------------| | Droid phone | orientationchange | 0 | 569 | 569 | | (to portrait) | resize | 0 | 320 | 320 | |----------------+-------------------+-------------+------------+--------------| | Samsung Galaxy | orientationchange | 0 | 400 | 400 | | Tablet | orientationchange | 90 | 400 | 400 | | (to landscape) | orientationchange | 90 | 400 | 400 | | | resize | 90 | 683 | 683 | | | orientationchange | 90 | 683 | 683 | |----------------+-------------------+-------------+------------+--------------| | Samsung Galaxy | orientationchange | 90 | 683 | 683 | | Tablet | orientationchange | 0 | 683 | 683 | | (to portrait) | orientationchange | 0 | 683 | 683 | | | resize | 0 | 400 | 400 | | | orientationchange | 0 | 400 | 400 | |----------------+-------------------+-------------+------------+--------------|

TA貢獻1772條經(jīng)驗 獲得超6個贊
如果你只關心 窗口尺寸 (典型情景) -而不是關于具體方向: 處理 resize
只有事件。 在你的處理程序中,采取行動 window.innerWidth
和 window.InnerHeight
只有。 不使用 window.orientation
-它不會在IOS上流行。
如果你真的關心 特定取向 :手柄 只
這個 resize
事件發(fā)生在Android上,以及 只
這個 orientationchange
事件發(fā)生在IOS上。 在你的處理程序中,采取行動 window.orientation
(和 window.innerWidth
和 window.InnerHeight
)
在桌面瀏覽器上開發(fā)時,這種只使用維度的方法也是有效的,這些瀏覽器可以模擬移動設備,例如Chrome 23。( window.orientation
在桌面瀏覽器上不可用)。 不需要global/anonymous-file-level-function-wrapper-level變量。
添加回答
舉報