我一直在試圖尋找通過CSS作爲背景圖像處理SVG元素的解決方案定位:SVG spritesheet與CSS
.icon.arrow-down
{
background-image: url('images/spritesheet.svg#arrow-down');
}
我使用:target
直接在SVG文件,以定位組合SVG spritesheet中的特定圖層(或「組」)。
<?xml version="1.0" encoding="utf-8" ?>
<svg class="icon" id="icon" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="50px" height="50px" viewBox="0 0 50 50">
<defs>
<style>
rect, line { shape-rendering: crispEdges; }
svg .icon { display: none; }
svg .icon:target { display: inline; }
</style>
</defs>
<!-- Arrows -->
<g class="icon" id="arrow-down" transform="translate(0,12.5)">
<path fill="#F00" d="M 0,0 50,0 25,25 z" />
</g>
<g class="icon" id="arrow-up" transform="translate(0,12.5)">
<path fill="#F00" d="M 0,25 50,25 25,0 z" />
</g>
...
</svg>
這對Firefox和IE9 +工作正常,但在Chrome它似乎忽略#profile
部分。但是,直接在瀏覽器中使用目標ID轉到SVG表格會生成正確的圖像。
這是Chrome瀏覽器在背景圖片中處理:target
的錯誤嗎?
我試圖避免必須將所有內容分離到自己的文件中,因此只有一個資源被下載,但我不知道它是可能的。
注意如何圖標都無法在Chrome中顯示,但在其他瀏覽器:http://jsfiddle.net/sYL5F/1/
除了返工所有圖像和css文件之外,是否還沒有解決方法? –
(我正在談論精靈貼圖的解決方法) –