在过去的 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