简体   繁体   English

内联SVG与CSS后台Sprite

[英]Inline SVG vs CSS-background Sprite

I'm creating a few pages in my app (currently running Angular on my front-end) that each contain a good deal of logos. 我正在应用程序中创建几个页面(当前在前端运行Angular),每个页面包含大量徽标。 I'm currently creating directives for each SVG logo (putting the inline SVG code in a template), which makes the code a lot more readable, but due to the number of logos, I'm starting to wonder if it would make more sense to create a CSS sprite instead. 我目前正在为每个SVG徽标创建指令(将内嵌SVG代码放入模板中),这使代码更具可读性,但是由于徽标的数量,我开始怀疑它是否更有意义而是创建一个CSS Sprite。

I would appreciate some thoughts from a performance perspective on this. 从性能的角度来看,我对此表示赞赏。 I know CSS background images get cached, so that's a plus. 我知道CSS背景图片会被缓存,所以这是一个加号。 SVG offers me more flexibility if I want to, say, color or size some specific logos differently, but if it's costing me a fair amount in terms of loading time I'd rather opt for the background image. SVG为我提供了更大的灵活性,例如,我想以不同的方式为某些特定徽标设置颜色或大小,但是如果在加载时间方面花了我很多钱,我宁愿选择背景图片。

I'm doing something like this for the company I work for. 我正在为我工​​作的公司做类似的事情。 They have hundreds of images that could easily compress into one SVG sprite. 它们具有数百张可以轻松压缩为一个SVG子画面的图像。 I guess the answer depends on the project, will it get bigger? 我猜答案取决于项目,它会变得更大吗? if yes I think taking the time to implement the sprite sheet now would be a great idea, also you could use it for icons. 如果是的话,我认为现在花点时间实现Sprite工作表将是一个好主意,也可以将其用于图标。 here is a great article on CSS tricks. 是一篇很棒的CSS技巧文章。

If you will start doing that I recommend using Grunt and implement grunt-svgstore it saves a ton of time creating the sprite sheet for you and it's pretty simple to implement, something like this. 如果您要开始这样做,我建议您使用Grunt并实现grunt-svgstore,这样可以节省大量时间为您创建Sprite表,并且实现起来非常简单,就像这样。

grunt.initConfig({
  svgstore: {
    options: {
      option like prefix ID with 'icon', indentation, etc.
      }
    },
    default : {
      files: {
        'dest/dest.svg': ['svgs/*.svg'], //destination, files to convert
      },
    },
  },
});

TL;DR: From a coding perspective I would say keep things simple, from a UX / UI perspective it depends on your use case. TL; DR:从编码角度讲,我想说要保持简单,从UX / UI角度看,这取决于您的用例。

I would go with the background image unless the interaction with the logos is a key element of the user journey. 我会选择背景图片,除非与徽标的交互是用户旅程的关键要素。

If for instance it is there to show the number of clients, their high profile or your status as a result, then an image is fine. 例如,如果在那里显示客户的数量,其知名度或您的状态,那么图像很好。 You won't be changing it that often and it's not necessary to set up the situation so you can manipulate the logos too much. 您不会经常更改它,也不需要设置情况,因此可以过多地处理徽标。

If you've got an integral interaction that is using the logos, a user hovers the logo changes colour and then on-click expands and shows more information - then the SVG route makes sense and is probably worth the effort. 如果您使用徽标进行了完整的交互,则用户将徽标悬停在徽标上会更改颜色,然后在单击时展开并显示更多信息-SVG路线很有意义,可能值得付出努力。


Keep things user centric - Is the cost in time to you going to gain enough of a user benefit? 让事情以用户为中心-您花费及时的成本获得足够的用户利益吗?

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

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