6

在过去的 4 个小时里,我一直在尝试找到一种方法来使用 Compass 和 sass 创建精灵图像,该方法还可以自动缩放每个单独的图像以与 background-size 属性一起使用。

我没有找到任何作品,不敢相信这有那么难。

有没有人有一个工作的例子?

编辑:这是我到目前为止

@mixin resize-sprite($map, $sprite, $percent) {
  $spritePath:    sprite-path($map);
  $spriteWidth:   image-width($spritePath);
  $spriteHeight:  image-height($spritePath);
  $width: image-width(sprite-file($map, $sprite));
  $height: image-height(sprite-file($map, $sprite));

  @include background-size(ceil($spriteWidth * ($percent/100)) ceil($spriteHeight * ($percent/100)));
  width: ceil($width*($percent/100));
  height: ceil($height*($percent/100));
  background-position: 0 floor(nth(sprite-position($map, $sprite), 2)  * ($percent/100) );
}

@mixin resize-sprite-set($map, $percent, $only...) {
  $name: sprite_map_name($map);

  @each $sprite in sprite_names($map) {
    @if length($only) == 0 or index($only, $sprite) != false {
      .#{$name}-#{$sprite} {
        @include resize-sprite($map, $sprite, $percent);
      }
    }
  }
}

mixin 没有返回错误。

$my-icons-spacing: 10px; // give some space to avoid little pixel size issues on resize

@import "my-icons/*.png";

$my-icons-sprite-dimensions: true;

@include all-my-icons-sprites;

// the fun part

.small-icons { // overriding all sprites
  @include resize-sprite-set($my-icons-sprites, 40); // 40% sized
}

.some-part-of-my-site {
  @include resize-sprite-set($my-icons-sprites, 40, logo, ok); // will create overrides only for sprites "logo" and "ok"
}

当我尝试编译时,我从上述实现中收到以下错误消息。通过 Prepros 应用程序。

remove ../images/my-icons-s9e77ab1ef1.png
   create ../images/my-icons-s9e77ab1ef1.png
    error style.scss (Line 62 of _mixins.scss: Undefined mixin 'resize-sprite-set'.)
identical ../css/style.css 
4

2 回答 2

7

我也对此做了一些研究。这个要点是我想出的: https ://gist.github.com/apauly/7917906

更新:

解决方案取决于三个关键部分:

  1. 刻度宽度
  2. 刻度高度
  3. 获取背景位置

0. 获取完整精灵和单个图标的尺寸:

$icon-file: sprite-file($map, $icon);
$icon-width: image-width($icon-file);
$icon-height: image-height($icon-file);

$sprite-file: sprite-path($map);
$sprite-width: image-width($sprite-file);
$sprite-height: image-height($sprite-file);

1. 考虑一个显示精灵作为背景的 div。设置background-size: 100%;以确保背景精灵覆盖了 div 的整个宽度。如果使用width: 100%;,结果将是这样的:

+----------------+
|--|             | 
|----------------| 
|--------|       | <--- This is the sprite image we want to display 
|------|         |
+----------------+

所以我们需要放大我们的背景来得到这样的东西:(overflow:hidden虽然 div 应该有)

+----------------+
|---------|             | 
|-----------------------| 
|----------------|      | <---
|-------------|         |
+----------------+

为此,只需将完整精灵的宽度除以单个图标的宽度:

width:percentage($sprite-width / $icon-width);

2. 这个基本上是受tkenny的一篇博文启发的:http: //inspectelement.com/tutorials/a-responsive-css-background-image-technique/

生成的 sass 代码是这样的:

display: block;
height: 0;
padding-bottom: percentage($icon-height / $icon-width);
background-size: 100%;

3. 剩下的只是一些基本的数学运算来计算精灵内部图标的顶部间距作为相对值:

以像素为单位从顶部获取空间(负值):

$space-top:floor(nth(sprite-position($map, $icon), 2));

Sass 需要一个 px 值

@if $space-top == 0 {
  $space-top: 0px
}

用百分比设置背景位置:

background-position:0 percentage(
  -1 * $space-top / ( $sprite-height - $icon-height )
);
于 2013-12-11T20:38:35.707 回答
2

这是一个用于调整精灵大小的mixin,效果很好

@mixin resize-sprite($map, $sprite, $percent) {
  $spritePath:    sprite-path($map);
  $spriteWidth:   image-width($spritePath);
  $spriteHeight:  image-height($spritePath);
  $width: image-width(sprite-file($map, $sprite));
  $height: image-height(sprite-file($map, $sprite));

  @include background-size(ceil($spriteWidth * ($percent/100)) ceil($spriteHeight * ($percent/100)));
  width: ceil($width*($percent/100));
  height: ceil($height*($percent/100));
  background-position: 0 floor(nth(sprite-position($map, $sprite), 2)  * ($percent/100) );
}

以及它来自的 github: https ://gist.github.com/darren131/3410875

于 2013-10-13T08:18:05.227 回答