1

Following the Spriting with Compass page instruction about the magical dimension functions, I'm trying to set the sizes of each sprite, using something like that:

  .top-bar-section {
    ul {
      & > li {
        // Generate the sprite map.
        $menu_icons-sprite-map: sprite-map("menu_icons/*.png", $layout: smart);

        // Set the background image.
        & > a:before {
          background: sprite-url($menu_icons-sprite-map) 0 0 no-repeat;
          display: inline-block;
          content: "";
          @include background-size(cover);
        }

        // Set the background position for each sprite.
        $menu_icons-sprite-names: sprite-names($menu_icons-sprite-map);
        @each $name in $menu_icons-sprite-names {
          &.menu_icons-#{$name} > a:before {
            background-position: sprite-position($menu_icons-sprite-map, $name);
            $height: menu_icons-sprite-height($name);
            $width: menu_icons-sprite-width($name);
            height: $height;
            width: $width;
          }
        }
      }
    }

However, the produced CSS looks like that:

...
.top-bar .top-bar-section ul > li.menu_icons-omino_001 > a:before {
  background-position: 0 0;
  height: menu_icons-sprite-height(omino_001);
  width: menu_icons-sprite-width(omino_001);
}
.top-bar .top-bar-section ul > li.menu_icons-omino_002 > a:before {
  background-position: 0 -64px;
  height: menu_icons-sprite-height(omino_002);
  width: menu_icons-sprite-width(omino_002);
}
...

It seems like the magical function is not created by Compass: am I missing something?

4

1 回答 1

2

当你sprite-map用来生成精灵时,魔法函数是没有定义的。

幸运的是,Compass为此定义了一个sprite-dimensions mixin:

@mixin sprite-dimensions($map, $sprite) {
   height: image-height(sprite-file($map, $sprite));
   width: image-width(sprite-file($map, $sprite)); 
}

在你的 SCSS 中,你可以简单地包含这个 mixin 来设置正确的大小:

&.menu_icons-#{$name} > a:before {
  background-position: sprite-position($menu_icons-sprite-map, $name);
  @include sprite-dimensions($menu_icons-sprite-map, $name);
}

如果您需要单独处理高度或宽度的值,请使用与 mixin 相同的函数调用:

$height: image-height(sprite-file($menu_icons-sprite-map, $name));
$width: image-width(sprite-file($menu_icons-sprite-map, $name));
于 2013-06-17T13:20:09.147 回答