【问题标题】:<map>-sprite-height and <map>-sprite-width not being translated<map>-sprite-height 和 <map>-sprite-width 没有被翻译
【发布时间】:2023-03-18 19:52:01
【问题描述】:

按照关于神奇维度函数的Spriting with Compass 页面说明,我正在尝试使用类似的方法设置每个精灵的大小:

  .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;
          }
        }
      }
    }

但是,生成的 CSS 看起来像这样:

...
.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);
}
...

好像不是指南针创造的神奇功能:我错过了什么吗?

【问题讨论】:

    标签: compass-sass css-sprites


    【解决方案1】:

    使用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));
    

    【讨论】:

    • 谢谢,我在任何地方都找不到这个!
    猜你喜欢
    • 2012-08-06
    • 2021-04-14
    • 2012-10-28
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    相关资源
    最近更新 更多