问题描述
document.addEventListener("orientationChanged", updateOrientation);
我想呼吁 updateOrientation的
功能,但功能只是不叫。我使用的JavaScript $ C $下是一样的。
I am trying to call a function on updateOrientation
, but the function is just not called. I am using javascript code for the same.
谁能帮助我与 orientationChange
使用javascript code。
Can anyone help me with orientationChange
using javascript code.
在此先感谢。
推荐答案
这不是那么容易的:我周围打了很多太:)
首先,你可能已经注意到,一些Android设备(如三星Galaxy Tab)将被检测为人像,而他们的景观,反之亦然。因此,首先你需要建立功能检测的基础上screen.width和screen.height的方向,如果没有检测到的iPad(iPad将总能正确显示的方向..我不是一个有趣的反正)。
然后,你必须将方向改变检测到超时,让整个环境相应地更改为新的方向一段时间后,触发回调函数。
因此,这里是我该怎么办..快乐分享吧:)
It's not so easy: I played around it a lot too :)
First you might have noticed that some android devices (for example samsung galaxy tab) will be detected as portrait while they are landscape and viceversa. So first you need to build functions to detect the orientation based on screen.width and screen.height if ipad is not detected (ipad will always show the orientation correctly .. I'm not a fun of it anyway).
Then you have to fire the callback function after a while that the orientation change is detected with a timeout to let the whole environment change accordingly to the new orientation.
So here is how I do .. happy to share it :)
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;
}
这篇关于在使用JavaScript Android的方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!