簡體   English   中英

用於檢測ipad和方向的Javascript解決方案

[英]Javascript solution to detect ipad, and orientation

是否有可靠的方法可以檢測到正在從iPad瀏覽網頁(無誤報)以及通過JavaScript定位的方向?

我認為這對第一部分有好處:

var isiPad = navigator.userAgent.match(/iPad/i) != null;

我想我已經找到了第二部分:

<button onclick="detectIPadOrientation();">What's my Orientation?</button>

<script type="text/javascript">
    window.onorientationchange = detectIPadOrientation;
    function detectIPadOrientation () {
        if ( orientation == 0 ) {
            alert ('Portrait Mode, Home Button bottom');
        } else if ( orientation == 90 ) {
            alert ('Landscape Mode, Home Button right');
        } else if ( orientation == -90 ) {
            alert ('Landscape Mode, Home Button left');
        } else if ( orientation == 180 ) {
            alert ('Portrait Mode, Home Button top');
        }
    }
</script>

我不知道它是否對所有iPad都可靠,例如包括新款較小的iPad。 所以我的問題是:這些功能在所有iPad上都能可靠地工作嗎? 會有誤報嗎?

依賴用戶代理字符串永遠不是一個好主意,因為它可能是被欺騙的。 但是,您應該考慮閱讀“響應式網頁設計”

http://en.wikipedia.org/wiki/Responsive_web_design

我建議您使用jQuery。 我感覺方向將是橫向或縱向。 我認為操作系統足夠聰明,可以根據您握持設備的方式來知道面對瀏覽器的方式。

考慮一下這個jQuery,這很可能符合您的要求。

$(window).resize(function() {
    /*  This function should be triggered when the orientation 
        changes since orientation essentially changes the window size. */

    if($(window).width() == ipadLandScapeWidth) {
        //Do this in landscape mode.
    } else {
        //Do this in portait mode.
    }
});

暫無
暫無

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

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