考察如下的 HTML 片段,通过 CSS 的 nth-child() 伪选择器实现列表的颜色循环,比如每三个一次循环。

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>

很容易地,我们能得出如下样式代码:

ul {
li:nth-child(3n + 1) {
color: indigo;
}
li:nth-child(3n + 2) {
color: red;
}
li:nth-child(3n + 3) {
color: green;
}
}

以上,只三种颜色循环,简单情况下可这样处理。但样式多起来之后,上面代码显得很臃肿且不易维护。

既然是使用 SASS,很容易想到可通过编写循环语句来将其简化。

循环过程就是遍历一个预设的颜色列表,为每一个颜色生成相应的样式。

List & Map

首先需要定义一个对象来保存预设的颜色列表,SASS 中的 Lists 可满足条件。

$colors: indigo, red, green;

使用上面的 list:

$colors: indigo, red, green;

@each $color in $colors {

.color-#{$color} {

color: $color;

}

}

编译后输出:

.color-indigo {
color: indigo;
} .color-red {

color: red;

} .color-green {

color: green;

}

当然,也可以定义 Map 类型,为每种颜色指定名字,这样使用的时候比较语义:

$colors: (
"indigo": indigo,
"red": red,
"green": green
); @each $name, $color in $colors {

.color-#{$name} {

color: $color;

}

}

索引

列表对象及其遍历如上,现在还需要在遍历过程中获得一个 index 索引值,用于生成 3*n+index

通过 index() 函数可以达到目的:

$colors: (indigo, red, green);

@each $color in $colors {

$index: index($colors, $color);

.color-#{$index} {

color: $color;

}

}

编译后输出:

.color-1 {
color: indigo;
} .color-2 {

color: red;

} .color-3 {

color: green;

}

生成 nth-child 样式

结合上面所有,可以得出生成 nth-child 样式的代码:

$colors: (indigo, red, green);

ul {

@each $color in $colors {

$index: index($colors, $color);

li:nth-child(3n + #{$index}) {

color: $color;

}

}

}

编译后输出:

ul li:nth-child(3n + 1) {
color: indigo;
}
ul li:nth-child(3n + 2) {
color: red;
}
ul li:nth-child(3n + 3) {
color: green;
}

注意到 nth-child(3n + #{$index}) 中的 3 是硬编码。既然我们的颜色是在一个数组中,所以我们是知道总个数的,也就能替换掉这里硬编码的写法。

通过 length() 函数可获得 list 的长度。改进后的代码如下:

$colors: (indigo, red, green);

ul {

@each $color in $colors {

$index: index($colors, $color);

li:nth-child(#{length($colors)}n + #{$index}) {

color: $color;

}

}

}

这样,如果后续有颜色增加,或顺序的调整,我们只需要更新 $colors 变量的值即可。

@mixin

进一步,我们可以将上面遍历生成 nth-child 的代码抽取成 mixin,这样可以被其他地方使用。

$colors: (indigo, red, green);

@mixin nth-color {

@each $color in $colors {

$index: index($colors, $color);

li:nth-child(#{length($colors)}n + #{$index}) {

color: $color;

}

}

} ul {

@include nth-color;

}

mixin 的优化

但像上面这样还不能达到完全公用,因为 mixin 中使用了 li 选择器,不是所有需要 nth-child 样式的地方,都使用的 li 元素,所以此处需要进行优化,使得 mixin 中的这部分内容灵活一点,以适用不同的使用环境。

首先,使用 & 父选择器便可快速解决,这样生成的样式便由包含这个 mixin 的选择器决定了。

$colors: (indigo, red, green);

@mixin nth-color {

@each $color in $colors {

$index: index($colors, $color);

&:nth-child(#{length($colors)}n + #{$index}) {

color: $color;

}

}

} ul {

li {

@include nth-color;

}

}

诚然,这样修改过后,确实可以将该 mixin 使用于多个地方了。

但考虑这么一种情况,因为上面列表比较简单,更加常见的情况是,列表中是另外复杂的元素,而不是单纯的一个元素,比如像下面这样:

<ul>
<li>
<div className="item">
<h3>title</h3>
<div>desc goes here...</div>
</div>
</li>
<li>
<div className="item">
<h3>title</h3>
<div>desc goes here...</div>
</div>
</li>
<li>
<div className="item">
<h3>title</h3>
<div>desc goes here...</div>
</div>
</li>
</ul>

现在想针对列表元素中的 h3 进行颜色的设置,即 nth-child 中设置的 color 只针对 h3 而不是整个 li 元素。

结合前面的优化,似乎可以这样写:

$colors: (indigo, red, green);

@mixin nth-color {

@each $color in $colors {

$index: index($colors, $color);

- &:nth-child(#{length($colors)}n + #{$index}) {

+ &:nth-child(#{length($colors)}n + #{$index}) h3{

color: $color;

}

}

} ul {

li {

@include nth-color;

}

}

编译后的结果:

ul li:nth-child(3n+1) h3 {
color: indigo;
}
ul li:nth-child(3n+2) h3 {
color: red;
}
ul li:nth-child(3n+3) h3 {
color: green;
}

从结果来看,达到了目标。但又在 nth-color 这个 mixin 中引入了 h3,使其变得不再通用,问题又回到了之前。

所以 & 只解决了 nth-child 父级嵌套的问题,对于 nth-child 后面还需要自定义选择器的情况,就需要用别的方式进一步优化。

@mixin 传参

mixin 是可以接收参数的,通过外部传递选择器进来,可以达到将 h3 从 mixin 从剥离的目的。

@mixin nth-color($child) {
@each $color in $colors {
$index: index($colors, $color);
&:nth-child(#{length($colors)}n + #{$index}) #{$child}{
color: $color;
}
}
} ul {

li {

@include nth-color("h3");

}

}

@mixin 将参数传递到外面

最后,nth-color 这个 mixin 还有一处不够灵活的地方,便是其样式内容。

现在在其中写死了只有一个 color: $color; 属性,也就是说,使用该 mixin 的地方只能用它来设置元素的字色。如果 mixin 能够将颜色传递出来,这样外部使用的时候就可以用到其他属性上,更加的灵活了。

SASS 确实也提供了这么种机制,即 mixin 能够身外传递参数,借助 @content 指令。

@content 指令指代调用 mixin 时书写的模式内容部分。如下的代码直观展示了其功能:

@mixin nth-color($child) {
@each $color in $colors {
$index: index($colors, $color);
&:nth-child(#{length($colors)}n + #{$index}) #{$child}{
color: $color;
+ @content;
}
}
} ul {

li {

- @include nth-color("h3")

+ @include nth-color("h3"){

+ border:1px solid orange;

+ };

}

}

编译后内容为:

ul li:nth-child(3n+1) h3 {
color: indigo;
border: 1px solid orange;
}
ul li:nth-child(3n+2) h3 {
color: red;
border: 1px solid orange;
}
ul li:nth-child(3n+3) h3 {
color: green;
border: 1px solid orange;
}

可以看到,外部书写的 border: 1px solid orange; 通过 @content 指代而放进了每个 nth-child 样式中。只是这个 border 的颜色现在还是写死的 orange,现在来将其设置成跟随相应的字色,即跟着 color 属性走。

当我们使用 @content(<arguments...>) 形式的 @content 时,可以传递一些参数,外部调用该 mixin 时需要使用如下形式来获取传递的参数:

@include <name> using (<arguments...>)

NOTE::这里的 using 语法只部分 SASS 实现(Dart Sass since 1.15.0, LibSass, Ruby Sass)中有支持,node-sass 还没有。

最终的 mixin

所以,最终版的代码为:

$colors: (indigo, red, green);

@mixin nth-color($child) {

@each $color in $colors {

$index: index($colors, $color);

&:nth-child(#{length($colors)}n + #{$index}) #{$child} {

color: $color;

@content ($color);

}

}

} ul {

li {

@include nth-color("h3") using($color) {

border: 1px solid #{$color};

}

}

}

编译后得到的 CSS:

ul li:nth-child(3n+1) h3 {
color: indigo;
border: 1px solid indigo;
}
ul li:nth-child(3n+2) h3 {
color: red;
border: 1px solid red;
}
ul li:nth-child(3n+3) h3 {
color: green;
border: 1px solid green;
}

总结

通过将生成 nth-child 样式的规则自动化的过程中,顺带使用了 SASS 中另外一些特性,

  • 定义和使用数组及对象
  • 数组及对象的遍历
  • 遍历过程中索引的获取
  • @mixin 的使用,参数的传递
  • @mixin@content 的使用
  • @mixin 中向外传递参数

相关资源

利用 SASS 简化 `nth-child` 样式的生成的更多相关文章

  1. 利用Helm简化Kubernetes应用部署(1)

    目录 利用Helm简化Kubernetes应用部署  Helm基础  安装Helm  使用Visual Studio 2019为Helm编写一个简单的应用    利用Helm简化Kubernetes应 ...

  2. 利用Helm简化Kubernetes应用部署(2)

    目录 定义Charts  使用Helm部署Demo  Helm常用操作命令  定义Charts 回到之前的“charts”目录,我们依次进行解读并进行简单的修改. Chart.yaml 配置示例: a ...

  3. 利用StringList对象来管理这些动态生成的对象

    如果程序需要动态创建大量的对象,那么我们可以利用StringList对象来管理这些动态生成的对象.1.创建StringList对象:OBJ := TStringList.Create; 2.保存动态生 ...

  4. 机器学习实战 - 读书笔记(14) - 利用SVD简化数据

    前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第14章 - 利用SVD简化数据. 这里介绍,机器学习中的降维技术,可简化样品数据. 基 ...

  5. 【工具篇】利用DBExportDoc V1.0 For MySQL自动生成数据库表结构文档

    对于DBA或开发来说,如何规范化你的数据库表结构文档是灰常之重要的一件事情.但是当你的库,你的表排山倒海滴多的时候,你就会很头疼了. 推荐一款工具DBExportDoc V1.0 For MySQL( ...

  6. 【机器学习实战】第14章 利用SVD简化数据

    第14章 利用SVD简化数据 SVD 概述 奇异值分解(SVD, Singular Value Decomposition): 提取信息的一种方法,可以把 SVD 看成是从噪声数据中抽取相关特征.从生 ...

  7. C# 利用VS自带的WSDL工具生成WebService服务类

    C# 利用VS自带的WSDL工具生成WebService服务类   WebService有两种使用方式,一种是直接通过添加服务引用,另一种则是通过WSDL生成. 添加服务引用大家基本都用过,这里就不讲 ...

  8. 机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据

    机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据 关键字:PCA.主成分分析.降维作者:米仓山下时间:2018-11-15机器学习实战(Ma ...

  9. 如何利用反射简化Servlet操作

    如何利用反射简化Servlet操作   一.反射的实现 新建类BaseServlet,继承HttpServlet(不需要在web.xml文件中配置) 1.在doPost()方法中处理请求乱码,并调用d ...

随机推荐

  1. 用C#实现的几种常用数据校验方法整理(CRC校验;LRC校验;BCC校验;累加和校验)

    CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗余检查(CRC)是一种数据传输检错 ...

  2. 10 UA池和代理池

    在Scrapy中,引擎和下载器之间有一个组件,叫下载中间件(Downloader Middlewares).因它是介于Scrapy的request/response处理的钩子,所以有2方面作用: (1 ...

  3. 【Offer】[27]【二叉树的镜像】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 请完成一个函数,输入一颗二叉树,该函数输出它的镜像. 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 ...

  4. 初学FPGA图像处理,开发板选择建议

    我用的是ZYNQ7010的开发板,纯粹是入门学习,对于初学者,使用较多的xlinx入门级的开发板一般是zynq7000系列,淘宝上买的较好的是黑金和米联科,我买的就是黑金的,个人觉得教程很少,学习资料 ...

  5. java树形结构工具类

    一.树形结构数据一般都是以子父id的形式存在数据库中,查询的时候只是带有子id和parent_id的List集合 并不是树形结构,所以我们现在要将普通的List集合转换为树结构数据(本工具类扩展操作树 ...

  6. 白话系列之IOC,三个类实现简单的Ioc

    前言:博客园上已经有很多IOC的博客.而且很多写的很好,达到开源的水平,但是对于很多新人来说,只了解ioc的概念,以及怎么去使用ioc.然后想更进一步去看源码,但是大部分源码都比较困难,当不知道一个框 ...

  7. 使用Hexo搭建个人博客并部署到GitHub或码云上全过程

    一.前言 如上图所示:GitHub有Github Pages,而码云也有码云 Pages 1.Github Pages或Gitee Pages是什么呢? Github Pages或者Gitee Pag ...

  8. C. Anadi and Domino

    题目链接:http://codeforces.com/contest/1230/problem/C C. Anadi and Domino time limit per test: 2 seconds ...

  9. Spark学习之RDDs介绍

    什么是RDDS? RDDS即Resilient distributed datasets(弹性分布式数据集). Spark中,所有计算都是通过RDDs的创建,转换,操作完成的. 一个RDD是一个不可改 ...

  10. java中String常见问题

    java中String常见问题 1.字符串比较==和equals ==:比较的是对象,判断两个引用的是否为同一内存地址(物理对象) equals:比较的是值 2.通过空白字符拆封字符串 str.spi ...