【问题标题】:Customizing the output of Compass sprites自定义指南针精灵的输出
【发布时间】:2013-06-11 01:48:30
【问题描述】:

我有一个顶部栏菜单(基于 Zurb 的基金会):

这是 SCSS:

.top-bar {
  .top-bar-section {
    ul {
      @import "menu-icons/*.png";
      @include all-menu-icons-sprites;
    }
  }
}

现在,这符合预期,但问题是我想在每个 li 元素中设置 a 元素的样式(实际上,我想将其应用于 .top-bar.topbar-section.ul.li.a:before)。

但是,由于该网站是在 WordPress 中构建的,因此菜单也是如此,我只能将类分配给 li 元素,我不知道如何使 Compass 的精灵工作。

我知道,我可以使用 walker 类自定义 WordPress 呈现菜单的方式,但我更愿意尝试找到一个解决方案,只需编写正确的 SCSS,只要有可能。

【问题讨论】:

  • 您是如何让 Foundation 和 SASS 在您的 WordPress 主题中工作的?我正在尝试做同样的事情。您是否使用过解释设置的教程或资源? (抱歉,这与您的原始问题相切。)
  • 其实没什么大不了的,如果从头开始的话。如果您知道如何构建主题,则不需要教程:只需创建一个包含基础的指南针项目。

标签: sass compass-sass css-sprites


【解决方案1】:

当默认输出不完全符合您的要求时,需要注意一些精灵辅助函数:

使用这些您可以将精灵样式应用于li 元素的子元素:

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

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

【讨论】:

  • 我遇到了一些麻烦才能让它工作,但最终我意识到我必须将最后一个选择器更改为&.menu-icons-#{$name} > a:before。谢谢@russ!
  • 啊,是的,我忘了精灵名称不包括文件夹名称。这种方法在类命名约定中允许更大的灵活性:)
猜你喜欢
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多