1

是否有可靠的方法来检测正在从 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 上可靠运行?会有任何误报吗?

4

1 回答 1

0

依赖用户代理字符串绝不是一个好主意,因为它可能会被欺骗。但是,您应该考虑阅读“响应式网页设计”

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.
    }
});
于 2012-12-06T13:58:45.550 回答