代码之家  ›  专栏  ›  技术社区  ›  dallen

有没有办法区分iphone3g/S访客和iphone4访客?

  •  6
  • dallen  · 技术社区  · 14 年前

    我想为iphone4用户加载更高分辨率的图片,但我知道检测用户的唯一方法是通过用户代理。但我的印象是,任何iOS4手机上MobileSafari的用户代理都是一样的。

    2 回复  |  直到 14 年前
        1
  •  3
  •   marc_s    9 年前

    您可以使用CSS3媒体查询来检测iphone4的设备像素比率(多少CSS像素等于一个物理显示像素)。然后,您可以使用CSS来加载更高分辨率的图像,而不是普通的图像。

    <head>

    <link rel="stylesheet" type="text/css" href="high_res.css" media="only screen and (-webkit-min-device-pixel-ratio: 2)" />
    

    这个 media

    #highres-if-possible {
        background-image: url(high_res_pic.png);
    }
    

    注意,您必须使用CSS在HTML中指定图像 background-image 属性而不是 src 属性。

    示例:对于60x50图像,请替换:

    <img id="highres-if-possible" src="low_res_pic.png">
    

    具有

    <img id="highres-if-possible" style="width: 60px; height: 50px; background-image: url(low_res_pic.png);">
    

    有关CSS3媒体查询和使用它们检测iPhone 4的更多信息,请参阅: http://blog.iwalt.com/2010/06/targeting-the-iphone-4-retina-display-with-css3-media-queries.html

    更新 :我遇到 this post this post <img style="background-image:url( http://.. .)"> 而不是 <img src="http://..."> )比我上面所说的还要多。

        2
  •  -1
  •   Olee Dee    14 年前

    使用HTTP\u USER\u代理字符串,您可能会知道它是哪一个。我从来没有试过,但如果你能开始的话。我还看到了一些php库,它们可以为您实现这一点。希望这有帮助。 干杯。