如何定位iPad和iPhone,但排除Android

时间:2012-04-02 21:41:24

标签: android iphone css ipad meta-tags

我一直在寻找这个答案,但我似乎找不到它。我只有iPhone和iPad - 我没有Android - 所以我不知道我的CSS是否甚至触及Droid?

这就是我的HTML和CSS中的内容:

HTML

<meta name="viewport" content="width=device-width,initial-scale=1, maximum-scale=1.0">

CSS

@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation: landscape) {
... CSS for iPad landscape
}

@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation: portrait) { 
... CSS for iPad portrait
}

@media only screen and (max-device-width: 480px) { 
... CSS for iPhone portrait and landscape
}

问题:

  1. Droid是否有特定的元标记?
  2. 我上面的CSS是否针对Droid?如果是这样,有没有办法可以修改它以便Droid忽略它?
  3. 是否有针对Droid的在线模拟器,以便我可以进行故障排除?
  4. 您可以在此处查看示例页面:http://www2.receivablesxchange.com/alternative-to-factoring-4

1 个答案:

答案 0 :(得分:3)

媒体查询用于显示区域和类型。它不会查看用户代理或类似的东西。使用JQuery + CSS,你能做的就是这样:

var _device = {};

if (navigator.userAgent.match(/iPad/i)) {
    _device.name = 'ipad';
    _device.type = 'mobile';
} else if (navigator.userAgent.match(/android/i)) {
    _device.name = 'android';
    _device.type = 'mobile';
} else {
    _device.type = 'desktop';
}

$('body').addClass(_device.name).addClass(_device.type);

现在,在CSS中,您只需添加适合您需求的正文类:

@media only screen and (max-device-width: 1024px) { 
    .ipad #myDiv {
        // css for #myDiv only seen on the iPad
    }

    .android #myDiv {
        // css for #myDiv only seen on the Android
    }

    .mobile #myDiv {
        // css for #myDiv available to iPad AND android but NOT desktop
    }
}