1. 程式人生 > >使用CSS來設定SVG中Use元素的樣式

使用CSS來設定SVG中Use元素的樣式

轉自:http://blog.csdn.net/xiaozhu2hao/article/details/47443031

參考:

用CSS給SVG <use>的內容新增樣式

http://www.w3cplus.com/svg/styling-svg-use-content-css.html

今天看到技術群中有人問svg中use元素可以設定樣式屬性不?因為他的symbol有200多種,但是隻是顏色不一樣。如果挨個定義,那svg檔案就會很長。自己也思考過這個問題後來看到http://tympanus.net/codrops/2015/07/16/styling-svg-use-content-css/這篇文章,收益匪淺。這篇文章講的比較詳細。就不在解釋原理了。直接上自己測試例子的程式碼,比較直觀。

      1 單純的svg檔案,將style直接寫在<svg>標籤元素下

  1. <svgxmlns="http://www.w3.org/2000/svg"style="display: none;"xmlns:xlink="http://www.w3.org/1999/xlink"width="2000"height="2000">
  2.    <styletype="text/css">
  3.      svg path {  
  4.           fill: inherit;  
  5.      }   
  6.      .codrops-1 {  
  7.         fill: #4BC0A5;  
  8.         color: #A4DFD1;  
  9.       }  
  10.     .codrops-2 {  
  11.       fill: #0099CC;  
  12.        color: #7FCBE5;  
  13.     }  
  14.     .codrops-3 {  
  15.        fill: #5F5EC0;  
  16.        color: #AEAFDD;  
  17.     }  
  18.   </style>
  19.   <def>
  20.     <symbolid="codrops"viewBox="0 0 23 30">
  21.         <pathclass="back"fill="#aaa"d="M22.63,18.261c-0.398-3.044-2.608-6.61-4.072-9.359c-1.74-3.271-3.492-5.994-5.089-8.62l0,0   c-1.599,2.623-3.75,6.117-5.487,9.385c0.391,0.718,0.495,1.011,0.889,1.816c0.143,0.294,0.535,1.111,0.696,1.43   c0.062-0.114,0.582-1.052,0.643-1.162c0.278-0.506,0.54-0.981,0.791-1.451c0.823-1.547,1.649-2.971,2.469-4.33   c0.817,1.359,1.646,2.783,2.468,4.33c0.249,0.47,0.513,0.946,0.791,1.453c1.203,2.187,2.698,4.906,2.96,6.895   c0.292,2.237-0.259,4.312-1.556,5.839c-1.171,1.376-2.824,2.179-4.663,2.263c-1.841-0.084-3.493-0.887-4.665-2.263   c-0.16-0.192-0.311-0.391-0.448-0.599c-0.543,0.221-1.127,0.346-1.735,0.365c-0.56-0.019-1.095-0.127-1.599-0.313   c1.448,3.406,4.667,5.66,8.447,5.78C19.086,29.537,23.469,24.645,22.63,18.261z"
    />
  22.         <pathclass="front"fill="#ddd"d="M6.177,11.659c0.212,0.367,0.424,0.747,0.635,1.136c0.164,0.303,0.333,0.606,0.512,0.927   c0.683,1.225,1.618,2.898,1.755,3.937c0.144,1.073-0.111,2.056-0.716,2.769c-0.543,0.641-1.315,1.014-2.186,1.067   c-0.87-0.054-1.643-0.43-2.186-1.067c-0.604-0.713-0.858-1.695-0.715-2.771c0.137-1.036,1.072-2.712,1.755-3.936   c0.18-0.32,0.349-0.623,0.513-0.927C5.752,12.404,5.964,12.026,6.177,11.659 M6.177,5.966L6.177,5.966   c-1.02,1.649-2.138,3.363-3.247,5.419c-0.932,1.728-2.344,3.967-2.598,5.88c-0.535,4.014,2.261,7.09,5.846,7.203   c3.583-0.113,6.379-3.189,5.845-7.203c-0.255-1.912-1.666-4.152-2.598-5.88C8.314,9.329,7.196,7.617,6.177,5.966L6.177,5.966z"/>
  23.     </symbol>
  24.   </def>
  25.   <usex="0"y="0"width="100"height="100"xlink:href="#codrops"class="codrops-1"/>
  26.   <usex="100"y="0"width="100"height="100"xlink:href="#codrops"class="codrops-2"/>
  27.   <usex="200"y="0"width="100"height="100"xlink:href="#codrops"class="codrops-3"/>
  28. </svg>
     2  如果用在Html中如下
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.        <styletype="text/css">
  5.         svg path {  
  6.            fill: inherit;  
  7.         }   
  8.         use.codrops-1 {  
  9.           fill: #4BC0A5;  
  10.           color: #A4DFD1;  
  11.         }  
  12.         use.codrops-2 {  
  13.          fill: #0099CC;  
  14.          color: #7FCBE5;  
  15.         }  
  16.         use.codrops-3 {  
  17.           fill: #5F5EC0;  
  18.           color: #AEAFDD;  
  19.         }  
  20.       </style>
  21.    </head>
  22.    <body>
  23.        <divstyle="width:1000px;height:1000px;border:2px solid #F00;overflow-y:scroll;">
  24.         <svgstyle="display: none;">
  25.           <symbolid="codrops"viewBox="0 0 23 30">
  26.              <pathclass="back"fill="#aaa"d="M22.63,18.261c-0.398-3.044-2.608-6.61-4.072-9.359c-1.74-3.271-3.492-5.994-5.089-8.62l0,0   c-1.599,2.623-3.75,6.117-5.487,9.385c0.391,0.718,0.495,1.011,0.889,1.816c0.143,0.294,0.535,1.111,0.696,1.43   c0.062-0.114,0.582-1.052,0.643-1.162c0.278-0.506,0.54-0.981,0.791-1.451c0.823-1.547,1.649-2.971,2.469-4.33   c0.817,1.359,1.646,2.783,2.468,4.33c0.249,0.47,0.513,0.946,0.791,1.453c1.203,2.187,2.698,4.906,2.96,6.895   c0.292,2.237-0.259,4.312-1.556,5.839c-1.171,1.376-2.824,2.179-4.663,2.263c-1.841-0.084-3.493-0.887-4.665-2.263   c-0.16-0.192-0.311-0.391-0.448-0.599c-0.543,0.221-1.127,0.346-1.735,0.365c-0.56-0.019-1.095-0.127-1.599-0.313   c1.448,3.406,4.667,5.66,8.447,5.78C19.086,29.537,23.469,24.645,22.63,18.261z"/>
  27.              <pathclass="front"fill="#ddd"d="M6.177,11.659c0.212,0.367,0.424,0.747,0.635,1.136c0.164,0.303,0.333,0.606,0.512,0.927   c0.683,1.225,1.618,2.898,1.755,3.937c0.144,1.073-0.111,2.056-0.716,2.769c-0.543,0.641-1.315,1.014-2.186,1.067   c-0.87-0.054-1.643-0.43-2.186-1.067c-0.604-0.713-0.858-1.695-0.715-2.771c0.137-1.036,1.072-2.712,1.755-3.936   c0.18-0.32,0.349-0.623,0.513-0.927C5.752,12.404,5.964,12.026,6.177,11.659 M6.177,5.966L6.177,5.966   c-1.02,1.649-2.138,3.363-3.247,5.419c-0.932,1.728-2.344,3.967-2.598,5.88c-0.535,4.014,2.261,7.09,5.846,7.203   c3.583-0.113,6.379-3.189,5.845-7.203c-0.255-1.912-1.666-4.152-2.598-5.88C8.314,9.329,7.196,7.617,6.177,5.966L6.177,5.966z"/>
  28.           </symbol>
  29.         </svg>
  30.         <svgheight="90px"width="69px">
  31.             <usexlink:href="#codrops"class="codrops-1"/>
  32.         </svg>
  33.         <svgheight="90px"width="69px">
  34.             <usexlink:href="#codrops"class="codrops-2"/>
  35.         </svg>
  36.         <svgheight="90px"width="69px">
  37.             <usexlink:href="#codrops"class="codrops-3"/>
  38.         </svg>
  39.         </div>
  40.     </body>
  41. </html>

效果如下圖