<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="ARTICLE @ XOOPS powered by FeedCreator" -->
<feed version="0.3" xmlns="http://purl.org/atom/ns#" xml:lang="zh-TW">
    <title>睡蓮‧池水間 :: 文章</title>
    <tagline>文章XML</tagline>
    <link rel="alternate" type="text/html" href="http://waterlily-lsl.com/modules/article/view.article.php/170/c2"/>
    <id>http://waterlily-lsl.com/modules/article/view.article.php/170/c2</id>
    <modified>2012-02-11T04:06:23+02:00</modified>
    <author>
        <name>waterlily at waterlily-lsl dot com</name>
    </author>
    <generator>ARTICLE @ XOOPS powered by FeedCreator</generator>
    <entry>
        <title>提升網站效能的 CSS Sprites</title>
        <link rel="alternate" type="text/html" href="http://waterlily-lsl.com/modules/article/view.article.php/170/c2"/>
        <created>2009-04-04T23:20:00+02:00</created>
        <issued>2009-04-04T23:20:00+02:00</issued>
        <modified>2009-04-04T23:20:00+02:00</modified>
        <id>http://waterlily-lsl.com/modules/article/view.article.php/170/c2</id>
        <author>
            <name>睡蓮</name>
        </author>
        <summary>類別: 語言集&lt;br /&gt;來源: (池水間)近來在為池水間進行網站優化，將XHTML和CSS代碼改良，其中CSS就首次使用了CSS Sprites技術，以提升網站效能。&lt;br /&gt;&lt;br /&gt;什麼是CSS Sprites？就是將多個圖片匯集到一個大圖片上，然後在CSS裡將每個使用不同背景圖路徑全部劃一改為集成圖片路徑，並給每個背景圖定位。&lt;br /&gt;&lt;br /&gt;例如下面的無序列表，每一個 li 標籤分別用五個不同背景圖：&lt;br /&gt;&lt;div class=&quot;xoopsCode&quot;&gt;&lt;p class=&quot;cTitle&quot;&gt;&lt;img class=&quot;icon-l&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon1.gif&quot; alt=&quot;&quot;/&gt;&lt;img class=&quot;icon-r&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon2.gif&quot; alt=&quot;&quot;/&gt;&lt;/p&gt;&lt;pre&gt;
&amp;lt;ul&amp;gt;
&amp;lt;li class=&amp;quot;image1&amp;quot;&amp;gt; image1 背景 &amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;image2&amp;quot;&amp;gt; image2 背景 &amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;image3&amp;quot;&amp;gt; image3 背景 &amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;image4&amp;quot;&amp;gt; image4 背景 &amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;image5&amp;quot;&amp;gt; image5 背景 &amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;在CSS裡給每個 li 設定不同背景圖片路徑：&lt;br /&gt;&lt;div class=&quot;xoopsCode&quot;&gt;&lt;p class=&quot;cTitle&quot;&gt;&lt;img class=&quot;icon-l&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon1.gif&quot; alt=&quot;&quot;/&gt;&lt;img class=&quot;icon-r&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon2.gif&quot; alt=&quot;&quot;/&gt;&lt;/p&gt;&lt;pre&gt;
.images1 {background: url(image1.png) no-repeat;}
.images2 {background: url(image2.png) no-repeat;}
.images3 {background: url(image3.png) no-repeat;}
.images4 {background: url(image4.png) no-repeat;}
.images5 {background: url(image5.png) no-repeat;}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;上面就用了五個不同的路徑，如將五個圖片匯集到一個圖片，路徑就只要劃一用同一個便可以了。這樣一來當網頁載入圖片時只需一次載入集成圖片，然後依據先前背景圖的定位而顯示出不同背景圖。&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong class=&quot;A_strong&quot;&gt;圖片匯集&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;background:url(/uploads/article/images/CSS_Sprites_3.png) no-repeat 50% 0;height:450px;width:900px&quot;&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong class=&quot;A_strong&quot;&gt;定位&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;當匯集到一個圖片之後，如何為每個 li 標籤分別指定 &lt;span style=&quot;color: #00ff00;&quot;&gt;5to1_image.png&lt;/span&gt; 裡面的不同背景圖呢？&lt;br /&gt;&lt;br /&gt;下圖的每一個紅框所示為每個 li 要用的背景：&lt;br /&gt;&lt;div style=&quot;background:url(/uploads/article/images/CSS_Sprites_3.png) no-repeat 50% -470px;height:300px;width:900px&quot;&gt;&lt;/div&gt;&lt;br /&gt;在為每個不同背景圖定位之前，須先給每個 li 標籤指定寬和高也就是100 px *  30px，如此定位才會準確。&lt;br /&gt;&lt;br /&gt;上面的image1 ，其灰色背景位處在 &lt;span style=&quot;color: #00ff00;&quot;&gt;5to1_image.png&lt;/span&gt; 裡的最上面，也就是最容易定位的地方：&lt;br /&gt;&lt;div class=&quot;xoopsCode&quot;&gt;&lt;p class=&quot;cTitle&quot;&gt;&lt;img class=&quot;icon-l&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon1.gif&quot; alt=&quot;&quot;/&gt;&lt;img class=&quot;icon-r&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon2.gif&quot; alt=&quot;&quot;/&gt;&lt;/p&gt;&lt;pre&gt;
.images1 {background: url(5to1_image.png) no-repeat &lt;span style=&quot;color: #ff0000;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;0&lt;/span&gt;;}
&lt;/pre&gt;&lt;/div&gt;上面代碼中紅色的0為x，藍色的0為y；x為水平位置，y為垂直位置。&lt;br /&gt;&lt;br /&gt;由於背景圖定位的預設值皆為0的關係，上面的代碼可以省略成如下：&lt;br /&gt;&lt;div class=&quot;xoopsCode&quot;&gt;&lt;p class=&quot;cTitle&quot;&gt;&lt;img class=&quot;icon-l&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon1.gif&quot; alt=&quot;&quot;/&gt;&lt;img class=&quot;icon-r&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon2.gif&quot; alt=&quot;&quot;/&gt;&lt;/p&gt;&lt;pre&gt;
.images1 {background: url(5to1_image.png) no-repeat;}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;好了，接下來給 image2 定位，其藍色背景位處在灰色的下面，見下圖：&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;background:url(/uploads/article/images/CSS_Sprites_3.png) no-repeat 70% -790px;height:250px;width:900px&quot;&gt;&lt;/div&gt;&lt;br /&gt;將 image2 的藍背景向上提升一格( 30px )至紅框位置，而水平位置維持不變：&lt;br /&gt;&lt;div class=&quot;xoopsCode&quot;&gt;&lt;p class=&quot;cTitle&quot;&gt;&lt;img class=&quot;icon-l&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon1.gif&quot; alt=&quot;&quot;/&gt;&lt;img class=&quot;icon-r&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon2.gif&quot; alt=&quot;&quot;/&gt;&lt;/p&gt;&lt;pre&gt;
.images2 {background: url(5to1_image.png) no-repeat 0 &lt;span style=&quot;color: #ffac00;&quot;&gt;-30px&lt;/span&gt;;}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;接著將位處在image 2 下面的 image 3 ，向上提升兩格至紅框，兩格就是60px。其他的image 4 和 image 5 則餘類推：&lt;br /&gt;&lt;div class=&quot;xoopsCode&quot;&gt;&lt;p class=&quot;cTitle&quot;&gt;&lt;img class=&quot;icon-l&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon1.gif&quot; alt=&quot;&quot;/&gt;&lt;img class=&quot;icon-r&quot; src=&quot;http://waterlily-lsl.com/themes/waterlilyLSL-GW/img/code-icon2.gif&quot; alt=&quot;&quot;/&gt;&lt;/p&gt;&lt;pre&gt;
.images3 {background: url(5to1_image.png) no-repeat 0 &lt;span style=&quot;color: #ffac00;&quot;&gt;-60px&lt;/span&gt;;}
.images4 {background: url(5to1_image.png) no-repeat 0 &lt;span style=&quot;color: #ffac00;&quot;&gt;-90px&lt;/span&gt;;}
.images5 {background: url(5to1_image.png) no-repeat 0 &lt;span style=&quot;color: #ffac00;&quot;&gt;-120px&lt;/span&gt;;}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;CSS Sprites 就是這樣用法，除了上面的多圖匯集成一圖之外，CSS Sprites亦可取代以往在photoshop繪製好設計圖後需作切圖的舊模式，利用CSS Sprites技術，便可不需切圖而直接使用了。&lt;br /&gt;</summary>
    </entry>
</feed>

