Windows Phone上的初始window.orientation始终为0

时间:2015-07-02 08:12:27

标签: javascript jquery html windows-phone-8 mobile

我试图在我的网站上检测设备方向。

orientationchange事件后,方向值似乎没问题。

$(window).on("orientationchange",function(event){alert(window.orientation);}) 

然而问题是页面启动时的初始window.orientation值,即使设备处于横向位置也始终为0。

$(document).ready(function(){alert(window.orientation);});

经过一段时间的延迟(我已经检查过)后它才会发生变化,只有在orientationchange事件发生后才会变为正确值。

有没有办法在页面启动时获得正确的方向?

3 个答案:

答案 0 :(得分:1)

除了chrome for android:https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation

之外,其他任何内容实际上都不支持该属性

根据您想要做什么,您可以使用CSS规则吗?

@media only screen and (max-width: 999px) {
  /* rules that only apply for canvases narrower than 1000px */
}

@media only screen and (device-width: 768px) and (orientation: landscape) {
  /* rules for iPad in landscape orientation */
}

@media only screen and (min-device-width: 320px) and (max-device-width:    480px) {
  /* iPhone, Android rules here */
}

答案 1 :(得分:0)

您是否尝试过这样的问题:

$(document).ready(function(){
    $(window).load(function(){
        alert(window.orientation);
    });
});

或者您可以手动获取设备的方向:

$(document).ready(function(){
    alert((($(window).width()>$(window).height()) ? 90 : 0));
});

答案 2 :(得分:0)

我刚刚在Windows Phone上测试过,它可以运行。您使用的是哪种手机/操作系统/浏览器?