簡體   English   中英

Windows Phone上的初始window.orientation始終為0

[英]initial window.orientation always 0 on Windows Phone

我正試圖在我的網站上檢測設備方向。

orientationchange事件后, orientationchange值似乎正常。

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

然而問題是頁面啟動時的初始window.orientation值,即使設備處於橫向位置也始終為0。

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

經過一些延遲后我沒有改變(我已經檢查過),只有在orientationchange事件之后它才會變為正確的值。

有沒有辦法在頁面啟動時獲得正確的方向?

看起來除了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 */
}

你有沒有嘗試過這樣的問題:

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

或者,您可以手動獲取設備的方向:

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

我剛剛在Windows Phone上進行了測試,然后就可以了。 您使用的是哪種手機/操作系統/瀏覽器?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM