简体   繁体   English

如何将SVG与咕unt和玉一起使用

[英]How to use SVG with grunt and jade

I am using svg files for my projects and I like to have them 'in-line' in the html so that I can use css/js to fiddle with them. 我正在为我的项目使用svg文件,我希望在html中将它们“内联”,以便可以使用css / js来摆弄它们。

I have 2 problems with this approach: 这种方法有两个问题:

  • I am using jade templating engine so I have to convert the xml to jade before adding them 我正在使用玉器模板引擎,因此必须在添加它们之前将xml转换为玉器
  • it really messes up my jade file adding a lot of xml code 它真的弄乱了我的玉器文件,添加了很多xml代码

So far the way I load my svg icons is the following: I put them in an assets folder and load them in jade like this: 到目前为止,我加载svg图标的方式如下:我将它们放在assets文件夹中,然后像这样将它们加载到玉器中:

          div
            img(src='/assets/svg/my_icon.svg')

But here, I cannot use js/css to style them. 但是在这里,我无法使用js / css设置样式。

Is there any way, using Grunt for example, to look up in my assets folder, grab the xml and populate my html code with it? 有什么办法,例如使用Grunt,在我的资产文件夹中查找,获取xml并用它填充我的html代码?

My approach to this is to use grunt-svgstore with the following settings in the Gruntfile.js 我的方法是在Gruntfile.js中使用具有以下设置的grunt- svgstore

svgstore: {
  default:{
    options: {
      prefix : 'icon-', // This will prefix each ID,
      svg: { // will add and overide the the default xmlns="http://www.w3.org/2000/svg" attribute to the resulting SVG
        viewBox : '0 0 100 100',
        xmlns: 'http://www.w3.org/2000/svg'
      }
    },
    files: {
      "<%= folders.src %>/jade/_svgDef.jade": ["<%= folders.src %>/svg/*.svg"]
    }
  }
}

Note : I'm compiling all of the SVGs in my folder directly into one jade file, that I include in my layout.jade like so: 注意 :我将文件夹中的所有SVG直接编译为一个jade文件,该文件包含在layout.jade中,如下所示:

.svgdef(style="display:none;")
  include _svgDef

For any SVG, grunt-svgstore creates nice IDs for you to include: 对于任何SVG,grunt-svgstore都会为您创建一个不错的ID,包括:

svg(viewBox="0 0 100 100", xmlns="http://www.w3.org/2000/svg")
    use(xlink:href="#icon-Twitter")

Which is cool, because now you can style this SVG within your CSS, while keeping your edited jade files free from clutter! 这很酷,因为现在您可以在CSS内设置此SVG的样式,同时使编辑的Jade文件保持混乱!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM