Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RETINA时代的前端优化

Avatar for sokamal sokamal
November 18, 2013

 RETINA时代的前端优化

RETINA屏幕的图片及图标处理

Avatar for sokamal

sokamal

November 18, 2013
Tweet

More Decks by sokamal

Other Decks in Programming

Transcript

  1. 5.2,Web Clip 指定几种分辨率的图片即可 <link rel="apple-touch-icon" href="touch-icon-iphone.png" /> <link rel="apple-touch-icon" sizes="72x72"

    href="touch-icon-ipad.png" /> <link rel="apple-touch-icon" sizes="114x114" href="touch-icon-iphone-retina.png" /> <link rel="apple-touch-icon" sizes="144x144" href="touch-icon-ipad-retina.png" /> 生成几种分辨率图标的解决方案 http://css-tricks.com/video-screencasts/122-the-state-of-favicons/ http://msdn.microsoft.com/en-us/library/ie/gg491740%28v=vs.85%29.aspx Web Clip https://developer.apple. com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplic ations/ConfiguringWebApplications.html
  2. 5.3.2 用 Javascript 替换图片 Retina屏幕用 Javascript 替换成大图,默认使用普通图片 <img src="low-res.png" data-src-retina="high-res.png"

    alt=""> var isRetina = (window.retina || window.devicePixelRatio > 1); if(isRetina){ $('[data-src-retina]').each(function(){ $(this).attr('src',$(this).attr('data-src-retina')); }) } // 代码用到了jQuery 这样做需要维护两套图片, 普通屏幕和以前一样,加载默认图片;Retina屏幕会先加载普通图片,然后再 加载一次大图,这里只考虑了屏幕情况,也是没有考 虑移动网络。
  3. 5.3.3 服务器端方案 网页顶部用脚本取得屏幕情况,保存到 cookies <script>document.cookie='resolution='+Math.max(screen.width,screen.height)+("devicePixelRatio" in window ? ","+devicePixelRatio :

    ",1")+'; path=/';</script> 服务器端设置rewrite 规则,判断cookies & 服务器端图片是否存在决定返回普通 图片还是@2x图片。 RewriteCond %{REQUEST_URI} ^/assets/images #RewriteCond %{REQUEST_URI} !^/assets/images/excluded RewriteCond %{HTTP_COOKIE} resolution=([^;,]+),2 RewriteCond %{REQUEST_URI} !@2x\.(jpe?g|gif|png)$ RewriteCond %{DOCUMENT_ROOT}/$1@2x.$2 -f RewriteRule ([^.]+)\.(jpe?g|gif|png)$ $1@2x.$2 这种方法前端处理简单,只需要指定图片宽或高就可以了,判断工作全部交 给服务器,缺点是也需要维 护两套图片,判断了屏幕情况,没有考 虑移动网络。
  4. 5.3.4 未来的更好方案 最新版的webkit实现了一个新的特性 srcset属性 <img src="low-res.png" srcset="high-res.png 2x"> 普通屏幕效果 rMBP效果

    srcset可以指定多个文件地址, 浏览器会根据屏幕 dpi,网络情况,加载合适的图片来显示,缺点只有一 个,目前只有webkit的nightly版本支持。 http://nightly.webkit.org/
  5. 5.4, 图标 5.4.1 sprite图片 .content a.fav-link { background: url(sprite.png) no-repeat

    0 -100px; } @media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2) { span.location, span.success, a.delete, .content a.fav-link { /* Reference the @2x Sprite */ background-image: url([email protected]); /* Translate the @2x sprite's dimensions back to 1x */ background-size: 200px 200px; } } 放大版的雪碧图设置尺寸为正常版本的大小。 要注意每个图标的位置都要一一对应。