[英]Orientation change in Android using javascript
document.addEventListener("orientationChanged", updateOrientation);
我試圖在updateOrientation
上調用一個函數,但是該函數沒有被調用。 我正在使用javascript代碼。
誰能幫我orientationChange
使用JavaScript代碼。
提前致謝。
這不是那么容易:我也經常玩它:)
首先,您可能已經注意到,某些Android設備(例如三星銀河標簽)在橫向時會被檢測為縱向。 因此,如果沒有檢測到ipad,首先需要構建函數來根據screen.width和screen.height來檢測方向(ipad始終會正確顯示方向。無論如何我都不覺得有意思)。
然后,您必須在一段時間后檢測到方向更改並超時,然后觸發回調函數,以使整個環境相應地更改為新的方向。
所以這就是我的方法..樂於分享:)
function orientation_changed ()
{
if ( is_portrait() )
{
//do something
}
else if ( is_landscape() )
{
// do something else
}
clearTimeout(window.t);
delete window.t;
}
window.t = undefined;
window.onorientationchange = function (event)
{
window.t = setTimeout('orientation_changed();', 250);
}
function is_landscape()
{
var uagent = navigator.userAgent.toLowerCase();
if ( uagent.search('ipad') > -1 )
{
var r = ( window.orientation == 90 || window.orientation == -90 );
}
else
{
var r = ( screen.width > screen.height );
}
return r;
}
function is_portrait()
{
var uagent = navigator.userAgent.toLowerCase();
if ( uagent.search('ipad') > -1 )
{
var r = ( window.orientation == 0 || window.orientation == 180 );
}
else
{
var r = ( screen.width < screen.height );
}
return r;
}
要測試本機的“ orientationchange”支持,請"orientationchange" in window
使用"orientationchange" in window
作為后備,使用onresize
事件並檢查window.outerHeight > window.outerWidth
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.