简体   繁体   English

改编iphone5的apple-touch-startup-image javascript代码?

[英]Adapt apple-touch-startup-image javascript code for iphone5?

I'm doing a webpage that uses the startup image feature for iOS devices. 我正在做一个使用iOS设备启动图像功能的网页。 For this task, I'm using a code placed on the footer that detects with javascript which iOS device is, and then loads the startup image for it. 对于此任务,我使用放置在页脚上的代码,该代码用javascript检测是哪个iOS设备,然后为其加载启动图像。 In this way, the site saves a lot of bandwith because it's downloading only one image instead of four. 这样,该站点节省了很多带宽,因为它仅下载一个映像,而不是四个。

But with the new screen size of the iPhone 5, my code needs some changes, but I can't figure out how to do these. 但是随着iPhone 5的新屏幕尺寸,我的代码需要进行一些更改,但是我不知道该怎么做。

This is the code: 这是代码:

<script>
(function(){
    var p,l,r=window.devicePixelRatio;
    if(navigator.platform==="iPad"){
        p=r===2?"http://example.com/ipad-portrait-retina.jpg":"http://example.com/ipad-portrait-non-retina.jpg";
        l=r===2?"http://example.com/ipad-landscape-retina.jpg":"http://example.com/ipad-landscape-non-retina.jpg";
        document.write('<link rel="apple-touch-startup-image" href="'+l+'" media="screen and (orientation: landscape)"/><link rel="apple-touch-startup-image" href="'+p+'" media="screen and (orientation: portrait)"/>');
    }
    else{
        p=r===2?"http://example.com/iphone-retina.jpg":"http://example.com/iphone-non-retina.jpg";
        document.write('<link rel="apple-touch-startup-image" href="'+p+'"/>');
    }
})
</script>

As you can see, this work is intended for the iPad/iPhone with variables for the device orientation and the device pixel ratio. 如您所见,这项工作是针对iPad / iPhone的,具有设备方向和设备像素比率的变量。 But the problem is that for the iPhone with retina display, there's no variable to determine if is the i5 or the i4/4S, so it just download the image for the 960x640 iPhone. 但是问题在于,对于带有视网膜显示屏的iPhone,没有变量可以确定是i5​​还是i4 / 4S,因此它只下载了960x640 iPhone的图像。

Do you have any clue on how to include a variable for device screen size? 您是否有关于如何包含设备屏幕尺寸变量的任何线索?

A better way to do this is to use media targeting, rather than JavaScript. 更好的方法是使用媒体定位,而不是JavaScript。

Done correctly, the iPhone will only ever retrieve the image appropriate to its screen size and pixel ratio, so there is no need to resort to JavaScript. 如果做得正确,iPhone将只能检索适合其屏幕尺寸和像素比率的图像,因此无需诉诸JavaScript。

Media Targeting 媒体定位

This works for iPhones; 这适用于iPhone;

<!-- iPhone 2G, 3G, 3GS -->
<link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px)" href="apple-touch-startup-image-320x460.png">
<!-- iPhone 4, 4S -->
<link rel="apple-touch-startup-image" media="(-webkit-device-pixel-ratio: 2) and (device-width: 320px) and (device-height: 480px)" href="apple-touch-startup-image-640x920.png">
<!-- iPhone 5 -->
<link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 568px)" href="apple-touch-startup-image-640x1096.png">

And for iPads; 对于iPad;

<!-- iPad 1, 2, Mini - Portrait -->
<link rel="apple-touch-startup-image" media="(device-width: 768px) and (orientation: portrait)" href="apple-touch-startup-image-768x1004.png">
<!-- iPad 1, 2, Mini - Landscape -->
<link rel="apple-touch-startup-image" media="(device-width: 768px) and (orientation: landscape)" href="apple-touch-startup-image-1024x748.png">
<!-- iPad 3, 4 - Portrait -->
<link rel="apple-touch-startup-image" media="(-webkit-device-pixel-ratio: 2) and (device-width: 768px) and (orientation: portrait)" href="apple-touch-startup-image-1536x2008.png">
<!-- iPad 3, 4 - Landscape -->
<link rel="apple-touch-startup-image" media="(-webkit-device-pixel-ratio: 2) and (device-width: 768px) and (orientation: landscape)" href="apple-touch-startup-image-2048x1496.png">

More information on these startup images can be found at Stuff & Nonsense . 有关这些启动映像的更多信息,请参见Stuff&Nonsense

This will cause the device to download only the image appropriate to it. 这将导致设备仅下载适合它的图像。

Measuring device height in JavaScript 用JavaScript测量设备高度

If you must use JavaScript for any reason (and I do advise against it), you can retrieve the device's height with window.screen.height . 如果出于任何原因必须使用JavaScript(我建议不要这样做),则可以通过window.screen.height检索设备的高度。 On 3.5-inch devices, it will be 480 , and on 4-inch devices, it is 568 . 在3.5英寸设备上为480 ,在4英寸设备上为568

For more information on measuring the viewport in JS, see ResponseJS' guide . 有关在JS中测量视口的更多信息,请参见ResponseJS的指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM