document.addEventListener("orientationChanged", updateOrientation);
我试图在updateOrientation
上调用一个函数,但该函数没有被调用。我正在使用javascript代码。
任何人都可以使用javascript代码帮助我orientationChange
。
提前致谢。
答案 0 :(得分:1)
这不是那么容易:我也玩了很多:)
首先你可能已经注意到一些Android设备(例如三星galaxy tab)将被视为肖像,而它们是风景和反之亦然。因此,首先你需要构建基于screen.width和screen.height检测方向的功能,如果没有检测到ipad(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;
}
答案 1 :(得分:0)
测试本地&#34; orientationchange&#34;支持,使用"orientationchange" in window
作为后备广告,请使用onresize
事件并检查window.outerHeight > window.outerWidth