Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
RETINA时代的前端优化
Search
sokamal
November 18, 2013
Programming
190
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
RETINA时代的前端优化
RETINA屏幕的图片及图标处理
sokamal
November 18, 2013
More Decks by sokamal
See All by sokamal
亚马逊的快速菜单
ikamal
1
120
jQuery最佳实践
ikamal
1
110
Less_css_介绍.pdf
ikamal
1
72
Other Decks in Programming
See All in Programming
FreeBSDでZabbixを動かす
kenkino
0
310
App Intentsのビルドプロセスを支える技術
kntkymt
0
400
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
160
Intent as Code
shoppingjaws
6
1k
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
150
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
290
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
200
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
1
150
wkhtmltopdfの次どうするか問題2026
willnet
2
1.5k
アクセシビリティから考える情報設計
high_g_engineer
0
380
AgentCore CLI で進化した AWS での AI エージェントの作り方 : 必要な機能を必要な時に
icoxfog417
PRO
3
360
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
210
Featured
See All Featured
Accessibility Awareness
sabderemane
1
210
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
2.9k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
830
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
910
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Test your architecture with Archunit
thirion
2
2.4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.8k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
65k
Transcript
RETINA时代的前端优化 ECD kamalyu
一,什么是Retina? “Retina”“视网膜屏”是苹果为最新的双密度的屏 幕起的名字, 其他厂商也有类似的屏幕,只是不叫这个名字。 最新的iPhone,iPad,MacBook Pro等设备用的 就是这类屏幕。
二,Retina屏幕带来了什么问题? MacBook Pro with Retina Display 显卡渲染出2880x1800个像素,其中每四个像素一组,输出原来屏幕的一个像素 显示的大小区域内的图像 用户所看到的图标与文字的大小与原来的1440x900分辨率显示屏相同,但精细度 是原来的4倍…由四个像素代替原来一个像素。
三,这个问题会影响哪些地方? 1,Favicon 2,Web Clip 3,图片 4,图标
四,怎样解决这个问题? 将原来的文件增大为原来的4倍(宽高都X2)
五,目前的几种解决方案 5.1,Favicon 默认图标是16x16,Retina屏幕需要32x32像素或者更大的。 对于IE6-10,用经典的32x32像素 ico格式图标放到网站根目录;其他浏览器可以 使用更大的比如96x96的png图标; <link rel="icon" href="path/to/favicon.png"> Favicon
的兼容问题:http://www.w3cplus.com/css/understand-the-favicon.html
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
5.3, 图片 5.3.1 使用两倍大的图片 <img src="my200x200image.jpg" width="100" height="100"> 这样处理最简单,也不用维护两套图片, 但是会让所有浏览器都加载大图片,
加载速度会受影响,移动网络流量也较大。
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屏幕会先加载普通图片,然后再 加载一次大图,这里只考虑了屏幕情况,也是没有考 虑移动网络。
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 这种方法前端处理简单,只需要指定图片宽或高就可以了,判断工作全部交 给服务器,缺点是也需要维 护两套图片,判断了屏幕情况,没有考 虑移动网络。
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.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; } } 放大版的雪碧图设置尺寸为正常版本的大小。 要注意每个图标的位置都要一一对应。
5.4.1 sprite图片 优点是兼容旧代码,用来支持Retina屏的新图标不会影响旧样式。 缺点很明显,要多维护一套图标,还要保证每个图标都对齐(这时有网格对齐的雪 碧图优点就突出了),雪碧图一般不会太大,对流量的影响基本可以不考虑。
5.4.2 icon font
5.4.2 icon font 优点,清晰锐利,可以设置不同颜 色,放大后不会失真,兼容性较好。 缺点,只能是单色图标。根据实践, ie8中会有锯齿,目前还没有较好的 解决方法。
RETINA时代的前端优化 Thank you ECD kamalyu