在 jQuery if/then 语句中识别 iOS 用户代理的简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7411662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:49:55  来源:igfitidea点击:

Simple way to identify iOS user agent in a jQuery if/then statement?

jqueryiosif-statementuser-agent

提问by technopeasant

Exactly like it sounds..

一模一样。。

Is there some magical and easy way to say:

有没有什么神奇而简单的说法:

    if (user agent is iOS) {
        if (browserRatio >=1.5) {
            $container.css('min-height', '360px');
        } else {
            $container.css('min-height', '555px');
        }
     }

回答by technopeasant

Found it.

找到了。

if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
    if (browserRatio >=1.5) {
        $container.css('min-height', '360px');
    } else {
        $container.css('min-height', '555px');
    }
}

回答by jimbo

I know you're asking about jquery in particular, but IMO, you almost certainly want to use CSS3 @mediaqueriesfor this. There's even support for testing for landscape or portrait orientation.

我知道您特别在询问 jquery,但是 IMO,您几乎肯定想为此使用CSS3@media查询。甚至支持横向或纵向测试。

@media (orientation:landscape) {
  .container_selector {
    min-height: 555px;
  }
}
@media (orientation:portrait) {
  .container_selector {
    min-height: 360px;
  }
}

Hope this helps!

希望这可以帮助!

回答by Aaron

In order for this to work you are going to need to define browserWidth, but yes it will work. Here I targeted iPad only.

为了使其工作,您将需要定义 browserWidth,但它会起作用。在这里,我仅针对 iPad。

    $(window).load(function(){
      var browserWidth = $(window).width(); 

      if (navigator.userAgent.match(/(iPad)/)) {
        if (browserWidth == 768) {
            $('.sectionI').css({'margin-left': '30px'});
        } else if (browserWidth == 1024)  {
            $('.sectionI').css({'margin-left': '0px'});
        }
      }
    });

回答by Aaron

For web app version try this.

对于网络应用程序版本试试这个。

if (
    ("standalone" in window.navigator) &&
    !window.navigator.standalone
    ){

    // .... code here ....
}

回答by Erando

To make sure this string doesn't get matched: Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 925) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537just change your code to

要确保此字符串不匹配:Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 925) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537只需将代码更改为

if (navigator.userAgent.match(/(\(iPod|\(iPhone|\(iPad)/)) {
    if (browserRatio >=1.5) {
        $container.css('min-height', '360px');
    } else {
        $container.css('min-height', '555px');
    }
}

回答by jimbo

Based on comments to my previous answer, it sounds like the real question is, "how do you hide the URL bar in an iPhone". To that question, I found this:

根据对我之前回答的评论,听起来真正的问题是“如何在 iPhone 中隐藏 URL 栏”。对于这个问题,我发现了这个:

How to Hide the Address Bar in MobileSafari:

如何在 MobileSafari 中隐藏地址栏

<body onload="setTimeout(function() { window.scrollTo(0, 1) }, 100);">...</body>