指南针精灵生成太多的CSS类

前端之家收集整理的这篇文章主要介绍了指南针精灵生成太多的CSS类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用指南针生成精灵图像.我有很多精灵图标,它产生了太多的CSS代码(背景图像的类选择器太多).所以让我们分析罗盘精灵代码

正如你在这里看到的http://compass-style.org/help/tutorials/spriting/

  1. @import "my-icons/*.png";
  2. @include all-my-icons-sprites;

会产生:

  1. .my-icons-sprite,.my-icons-delete,.my-icons-edit,.my-icons-new,.my-icons-save { background: url('/images/my-icons-s34fe0604ab.png') no-repeat; }
  2.  
  3. .my-icons-delete { background-position: 0 0; }
  4. .my-icons-edit { background-position: 0 -32px; }
  5. .my-icons-new { background-position: 0 -64px; }
  6. .my-icons-save { background-position: 0 -96px; }

如果您看到我使用这种方式:< div class =“my-icons-sprite my-icons-delete”>< / div>

我希望Compass生成代码

  1. .my-icons-sprite { background: url('/images/my-icons-s34fe0604ab.png') no-repeat; }
  2.  
  3. .my-icons-delete { background-position: 0 0; }
  4. .my-icons-edit { background-position: 0 -32px; }
  5. .my-icons-new { background-position: 0 -64px; }
  6. .my-icons-save { background-position: 0 -96px; }

除了每个新图像,它还会添加背景和背景位置.引起太多选择者.

有配置吗?

谢谢

解决方法

你有没有尝试过Compass的这个片段?
  1. $icons: sprite-map("icons/*.png");
  2.  
  3. i{
  4. background: $icons;
  5. display: inline-block; // or block
  6. }
  7.  
  8. @each $i in sprite_names($icons){
  9. .icn-#{$i}{
  10. background-position: sprite-position($icons,$i);
  11. @include sprite-dimensions($icons,$i);
  12. }
  13. }

此示例使用< i>< / i> -tag,其中包含前缀icn-的类与图标文件夹中单独的.png文件文件名相结合.像这样:

  1. <i class="icn-delete"></i>

生成的CSS如下所示:

  1. i {
  2. background: url('/path/to/generated/spritemap/my-icons-xxxxxxxxxxx.png');
  3. display: inline-block;
  4. }
  5. .icn-delete {
  6. background-position: 0 0;
  7. height: 32px; // assuming the width is 32px
  8. width: 32px; // assuming the height is 32px
  9. }
  10. .icn-edit{
  11. background-position: 0 -32px;
  12. height: 32px; // assuming the width is 32px
  13. width: 32px; // assuming the height is 32px
  14. }
  15. .icn-new {
  16. background-position: 0 -64px;
  17. height: 32px; // assuming the width is 32px
  18. width: 32px; // assuming the height is 32px
  19. }
  20. ...
  21. ..
  22. .

尽管如此,我还没有弄清楚如何将其与Compass’ Magic Selectors结合使用.

当您需要不同的状态时,Magic Selectors工作得非常好(:hover,:active,:target).您所要做的就是将文件命名为:filename_state.png(delete_hover.png,delete_active.png等). Compass’Magic Selectors然后自动生成css:hover,:active和:target(delete:hover,delete_hover和delete-hover).通过这种方式,您可以自由选择如何表示状态变化.

在我的第一个示例中,如果您的文件名具有hover / active状态的后缀,则代码段只会像这样写入CSS:

  1. .icn-edit_hover {
  2. background-position: -32px -32px;
  3. height: 32px;
  4. width: 32px;
  5. }

我真的很喜欢打印这个:

  1. .icn-edit:hover,.icn-edit_hover,.icn-edit-hover{
  2. background-position: 0 -32px;
  3. height: 32px;
  4. width: 32px;
  5. }

像传统的Compass’ Magic Selectors一样.任何的想法?

猜你在找的CSS相关文章