css - SASS/SCSS 对象键值循环

标签 css loops sass compass-sass

<分区>

看看这个例子:

@include font-face('Entypo', font-files('entypo.woff'));

.icon {
  display: inline;
  font: 400 40px/40px Entypo;
}

.icon-star {
  @extend .icon;

  &:after {
    content: "\2605";
  }
}

.icon-lightning {
  @extend .icon;

  &:after {
    content: "\26A1";
  }
}

我想让事情尽可能干燥,所以我想知道以下是否可行,如果可行,如何实现?

@include font-face('Entypo', font-files('entypo.woff'));

.icon {
  display: inline;
  font: 400 40px/40px Entypo;
}

$icons {
  $star: "\2605";
  $lightning: "\26A1";
}

@each $icon in $icons {
  $key = $icon{key}; // ???
  $value = $icon{value}; // ???

  .icon-#{$key} {
    @extend .icon;

    &:after {
      content: $value;
    }
  }
}

最佳答案

Sass 3.3(2014 年 3 月 7 日发布)现在允许您使用 map :

@include font-face('Entypo', font-files('entypo.woff'));

.icon {
  display: inline;
  font: 400 40px/40px Entypo;
}

$icons: (
  star: "\2605",
  lightning: "\26A1"
);

@each $key, $value in $icons {
  .icon-#{$key} {
    @extend .icon;

    &:after {
      content: $value;
    }
  }
}

关于css - SASS/SCSS 对象键值循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16083292/

相关文章:

node.js - 如何使用 webpack 通过 node-sass 和 sass-loader 引用项目的根文件夹

angular - 使用 ~(波形符)导入 SCSS 文件在 Angular 6 中不起作用

html - 移动 li 文本但不移动列表类型图像

html - Internet Explorer 将 div 宽度视为 100%?

java - 使用适当的变量值编写代码

Java - 可选循环

html - 使用 html 和 css 设计复杂的表格布局

html - 当我将此 ul 添加到 div 时,它之前的 div 位置较低。我怎样才能让他们坐在同一高度

java - Java 中循环的性能

css - 您可以导入一个 css 文件并使用 sass 迭代每个规则并附加 !important 吗?