使用svgstore生成SVG Sprite

step1:新建一個文件夾svg

svg裏新建src文件用來保存要合併的svg文件

step2 新建package.json

package.json :
{ "devDependencies": { "grunt": "^1.0.1", "grunt-svgstore": "^1.0.0" } }
npm install安裝依賴包

step3 新建Gruntfile.js
module.exports = function(grunt) {
    // 配置
    grunt.initConfig({
        svgstore: {
            options: {
              prefix : 'icon-', 
              svg: { 
                viewBox : '0 0 200 200',
                xmlns: 'http://www.w3.org/2000/svg'
              },
              includedemo:true,
            },
            default : {
              files: {
                'demo/demo-svg.svg': ['src/*.svg'],
              },
            }

        }
    });
    // 載入grunt-svgstore
    grunt.loadNpmTasks('grunt-svgstore');
    // 註冊任務
    grunt.registerTask('default', ['svgstore']);
};
step4 命令行輸入grunt 和並svg文件

得到需要的合併後的文件
在這裏插入圖片描述

demo-svg-demo.html裏演示了引用方法
<!doctype html>
<html>
  <head>
    <style>
      svg{
       width:50px;
       height:50px;
       fill:black !important;
      }
    </style>
  <head>
  <body>
    <svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg" style="width:0;height:0;visibility:hidden;"><defs><style type="text/css"/></defs><symbol viewBox="0 0 1024 1024" id="icon-addgroup"><title>addgroup</title><path d="M580.722174 437.990403 580.722174 78.171384 436.794158 78.171384 436.794158 437.990403 76.975139 437.990403 76.975139 581.918419 436.794158 581.918419 436.794158 941.737438 580.722174 941.737438 580.722174 581.918419 940.542216 581.918419 940.542216 437.990403Z" p-id="3674"/></symbol></svg>

        <svg>
          <use xlink:href="#icon-addgroup" />
        </svg>

  </body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章