--- 博客已遷移至: http://www.sealyu.com/blog
一,關(guān)于CSS Sprite CSS Sprites是一種網(wǎng)頁圖片應(yīng)用處理方式。它允許你將一個(gè)頁面涉及到的所有零星圖片都包含到一張大圖中去,這樣一來,當(dāng)訪問該頁面時(shí),載入的圖片就不會(huì) 像以前那樣一幅一幅地慢慢顯示出來了。對(duì)于當(dāng)前網(wǎng)絡(luò)流行的速度而言,不高于200KB的單張圖片的所需載入時(shí)間基本是差不多的,所以無需 顧忌這個(gè)問題。 按照yahoo的rules for high performance web sites的原則,應(yīng)當(dāng)較少Client與Server端間 的HTTP Request次數(shù)。通過CSS Sprites方法將多張圖片組裝成單獨(dú)的一張圖片,可以有效減少HTTP請(qǐng)求 的次數(shù)。 當(dāng)整幅圖片載入完成后,你就可以使用CSS方法通過設(shè)置背景位置的方式完成所需圖片的準(zhǔn)確調(diào)用。
加速的關(guān)鍵,不是降低重量,而是減少個(gè)數(shù)。傳統(tǒng)切圖講究精細(xì),圖片規(guī)格越小越好,重量越小越好,其實(shí)規(guī)格大小無所謂,計(jì)算機(jī)統(tǒng)一都按byte計(jì)算。客戶端每顯示一張圖片都會(huì)向服務(wù)器發(fā)送請(qǐng)求,所以,圖片越多請(qǐng)求次數(shù)越多,造成延遲的可能性也就越大。
二,CSS Sprite的使用
有幾篇關(guān)于CSS Sprites的文章,基本上把其原理和機(jī)制說明得很清楚。
What Are CSS Sprites?
How to create CSS sprites
Creating Rollover Effects with CSS Sprites
Building a Dynamic Banner with CSS Sprites
High Performance Web Sites中關(guān)于CSS Sprites的內(nèi)容3.2. CSS Sprites
三,CSS Sprite的例子
[原文:http://blog.rexsong.com/?p=746#comments]
1. 圖片限制(Image Slicing)
典型如文本編輯器,小圖標(biāo)特別多,打開時(shí)一張張跑出來,給用戶的感覺很不好。如果能用一張圖解決,則不會(huì)有這個(gè)問題,比如百度空間、163博客、Gmail都是這么做的。
Image Slicing’s Kiss of Death http://www.alistapart.com/articles/sprites
2. 單圖轉(zhuǎn)滾(Single-image Rollovers)
觸發(fā)切換圖片的需求,傳統(tǒng)方案得重新請(qǐng)求新圖片,因?yàn)榫W(wǎng)絡(luò)問題經(jīng)常造成停留或等待。如果能把多種狀態(tài)合并成一張圖,就能完美解決,然后再使用背景圖技術(shù)模擬動(dòng)態(tài)效果。
ColorScheme Ratings http://demo.rexsong.com/200608/colorscheme_ratings/
3. 延長(zhǎng)背景(Extend Background Image)
如果圖片的某邊可以背景平鋪無限延長(zhǎng),則不需要每個(gè)角、每條邊單獨(dú)搞出來,圖片能少一個(gè)就少一個(gè)。其實(shí),這個(gè)理論還可以擴(kuò)展到四角容器里,好處是能大大簡(jiǎn)化HTML Structure。
Extend Background Image http://demo.rexsong.com/200705/extend_background_image/
綜合案例
Google Korea(1和2技巧) http://demo.rexsong.com/200705/google_korea/
CSS Menus(2和3技巧) http://demo.rexsong.com/200705/css_background_menus/
四,CSS Sprites的問題
由于IE6存在的background的flicker問題IE6/Win, background image on <a>, cache=‘check every visit’: flicker!,有人針對(duì)此問題提出了解決方案Fast Rollovers Without Preload
關(guān)于IE6的flicker問題,fivesevensix.com上有一篇很不錯(cuò)的研究文章Minimize Flickering CSS Background Images in IE6
另外:brunildo.org的CSS tests and experiments是關(guān)于css各種功能不錯(cuò)的參考手冊(cè)和測(cè)試工具。
http://www.peachpit.com/articles/printerfriendly.aspx?p=447210&rl=1
CSS Sprites: Image Slicing’s Kiss of Death
http://www.alistapart.com/articles/sprites/
CSS Sprites Generator
http://www.csssprites.com/
http://spritegen.website-performance.org/
Fast Rollovers Without Preload
http://wellstyled.com/css-nopreload-rollovers.html
JavaScript Sprite Animation Using jQuery
http://www.sitepoint.com/blogs/2007/07/20/javascript-sprite-animation-using-jquery/
http://www.sitepoint.com/blogs/2007/07/05/css-using-percentages-in-background-image/
http://fatagnus.com/how-to-create-css-sprites/
http://www.devarticles.com/c/a/Web-Style-Sheets/Creating-Rollover-Effects-with-CSS-Sprites/
http://www.devarticles.com/c/a/Web-Style-Sheets/Building-a-Dynamic-Banner-with-CSS-Sprites/
CSS Sprites and IE/Win Flicker Issue
http://www.brajeshwar.com/2006/css-sprites-and-iewin-flicker-issue/
css用法測(cè)試工具:CSS tests and experiments
http://www.brunildo.org/test/index.html
Powered by: BlogJava Copyright © seal