sass - compass Sprite 图像和比例

标签 sass compass-sass css

我花了 4 个小时试图找到一种使用 Compass 和 sass 创建 sprite 图像的方法,该图像还可以自动缩放每个单独的图像以与 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 

最佳答案

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

更新:

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

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

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

$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. 考虑一个显示 Sprite 作为其背景的 div。设置 background-size: 100%; 以确保背景 Sprite 覆盖 div 的整个宽度。 如果使用 width: 100%;,结果将是这样的:

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

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

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

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

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. 剩下的只是一些基本的数学运算,用于计算 Sprite 内部图标的顶部间距作为相对值:

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

$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 )
);

关于sass - compass Sprite 图像和比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19341929/

相关文章:

css - 在 netbeans 上使用 Sass 和 compass 导入

css - SASS/Compass Math 使用动态宽度

css - 如何在 Visual Studio 2012 中反射(reflect)运行时对 CSS 的更新?

html - 页面导航不会停留在一行

html - 防止破坏负数

css - 使用 sass 7-1 架构在代码库中使用的类的样式放在哪里

javascript - 图标干扰按钮点击事件

javascript - 使用 GWT/Java/JavaScript 滚动到可滚动对象的特定位置

css - SASS 错误 : Incompatible units: 'px' and 'px*px'

sass - 编译成 CSS 时如何覆盖 SCSS 变量