【问题标题】:Compass Sprite Image and Scale指南针精灵图像和比例
【发布时间】:2013-10-20 22:30:46
【问题描述】:

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

【问题讨论】:

  • 不知道是否有帮助,但this seems related。也许你可以告诉我们什么是行不通的?
  • 我添加了我正在使用的代码。

标签: sass compass-sass css


【解决方案1】:

我也对此进行了一些研究。这个要点是我想出的: 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 
|------|         |
+----------------+

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

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

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

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 需要一个像素值

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

用百分比设置背景位置:

background-position:0 percentage(
  -1 * $space-top / ( $sprite-height - $icon-height )
);

【讨论】:

    【解决方案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

    【讨论】:

      猜你喜欢
      • 2012-09-26
      • 2013-09-03
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      相关资源
      最近更新 更多