js / jQuery可以确定iPhone的方向吗? [英] Can js/jQuery determine the orientation of the iPhone?

查看:263
本文介绍了js / jQuery可以确定iPhone的方向吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于好奇,我一直在使用jQuery来确定浏览器的屏幕大小,我发现屏幕大小可以用来确定访问者是否使用iPhone / iTouch查看网站。 / p>

所以我使用以下内容来测试:

  $(文件).ready(

function(){

var screenX = screen.width,
screenY = screen.height;

alert (X:+ screenX +Y:+ screenY);

if(screenX == 320&& screenY == 396){
$('div #wrap) ')。css('background-color','#f00');
}

else if(screenY == 320&& screenX == 396){
$('div#wrap')。css('background-color','#0f0');
}
}
);

在通过iPhone查看页面时,我注意到尺寸始终如一(无论方向如何):



x:320,y:396



这与方向无关。我还没有尝试使用 onChange 事件来检测更改(主要是因为我在jQuery上仍然是新手),但我想知道是否有通过jQuery或普通javascript确定iPhone / iTouch的方向?

解决方案

window.orientation 将给你一个表示旋转的整数。您可以通过向正文添加事件来收听方向更改:

 < body onorientationchange =updateOrientation();> ; 






由OP编辑,只是关闭 - 链接在某个时刻死亡或被移动的可能性...

  Value |说明
------- + ------------------------------------- ------------------------------------------
0 |纵向。这是默认值。
-90 |屏幕顺时针转动时的横向方向。
90 |逆时针旋转屏幕的横向方向。
180 |屏幕纵向翻转时的纵向方向。 iPhone目前不支持此值。


Out of curiosity I've been playing with jQuery to determine the browser's screen size, and it occurred to me that screen size could be used to determine whether or not a visitor was using an iPhone/iTouch to view the site.

So I used the following to test this:

$(document).ready(

    function() {

        var screenX = screen.width,
            screenY = screen.height;

        alert("X: " + screenX + " Y: " + screenY);

        if (screenX == 320 && screenY == 396) {
            $('div#wrap').css('background-color','#f00');
        }

        else if (screenY == 320 && screenX == 396) {
            $('div#wrap').css('background-color','#0f0');
        }
    }
);

On viewing the page via iPhone, I notice that the dimensions are consistently (regardless of orientation):

x: 320, y: 396

This is regardless of orientation. I haven't, as yet, attempted to use an onChange event to detect changes (mainly because I'm still so new at jQuery), but I wondered if there was a way to determine, via jQuery or plain javascript, the iPhone/iTouch's orientation?

解决方案

window.orientation will give you an integer that denotes the rotation. You can listen for orientation changes by adding an event to the body:

<body onorientationchange="updateOrientation();">


Edited by OP, just on the off-chance that the link dies or gets moved at some point...

Value  |  Description
-------+-------------------------------------------------------------------------------
 0     |  Portrait orientation. This is the default value.
-90    |  Landscape orientation with the screen turned clockwise.
 90    |  Landscape orientation with the screen turned counterclockwise.
 180   |  Portrait orientation with the screen turned upside down. This value is currently not supported on iPhone.

这篇关于js / jQuery可以确定iPhone的方向吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆