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

完全喜欢它听起来..

有没有一些神奇而简单的方法来说:

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

find了。

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

我知道你特别要问jquery,但是IMO,你几乎可以肯定地想用CSS3 @media查询 。 甚至还支持横向或纵向testing。

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

希望这可以帮助!

为了这个工作,你将需要定义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'}); } } }); 

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

 if ( ("standalone" in window.navigator) && !window.navigator.standalone ){ // .... code here .... } 

为了确保这个string没有得到匹配: 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'); } } 

基于对我以前的答案的评论,这听起来像真正的问题是,“你怎么隐藏在iPhone的URL栏”。 对于这个问题,我发现这个:

如何隐藏MobileSafari中的地址栏 :

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