if语句在while循环中.它为什么这样做?

cat*_*cat 0 javascript iphone jquery while-loop ipad

我用这个:

//if in landscape mode
while(window.orientation == 90 || window.orientation == -90) {
                    if(!$('#page' + hiddenNextPage).hasClass('showUp')) {
                        $('#page' + hiddenNextPage).addClass('showUp');
                        $('#page' + hiddenNextPage).css('margin-left', '300px');
                    };
                }

//if in portrait mode
while(window.orientation == 0) {
                    if($('#page' + hiddenNextPage).hasClass('showUp')) {
                        $('#page' + hiddenNextPage).removeClass('showUp');
                        $('#page' + hiddenNextPage).css('margin-left', '5px');
                    };
                };
Run Code Online (Sandbox Code Playgroud)

但它使我的页面不再加载..并且它需要这么长的时间来加载它.它有什么问题吗?

是否有更好的方法可以不使用while循环不断检查方向是否已更改?

这是为ipad/iphone
谢谢!

jhu*_*man 6

我没有看到你的while循环的主体正在修改while测试.这会产生无限循环.


ste*_*son 5

由于您只是在更改样式,因此您可以使用单独的样式表来处理纵向和横向...

<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
Run Code Online (Sandbox Code Playgroud)

编辑

还有一个onorientationchange事件window允许您在方向更改时运行代码而不是不断轮询更改...

window.onorientationchange = function (event)
{
    if (window.orientation == 90 || window.orientation == -90)
    {
        ...
    }
    else
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)