这端代码能在scss里面优化吗?

$color_1:black;
$color_2:red;
$color_3:yellow;

.txt_1{
  color: $color_1;
}
.txt_2{
  color: $color_2;
}
.txt_3{
  color: $color_3;
}
阅读 1.9k
2 个回答

可以这样写。

$color_1:black;
$color_2:red;
$color_3:yellow;
$colorlist: $color_1 $color_2 $color_3;
@for $i from 1 through 3 {
  .txt_#{$i} { color: nth($colorlist, $i); }
}

命名可以语义化一些,方便使用。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进