作为一个CSS预处理器,Sass正受到越来越多的青睐,诸如Github、Codepen、CSS-Tricks、SitePoint、w3cplus等网站采用Sass组织、管理CSS文件,Sass正在逐渐成为事实上的CSS预处理器行业标准。接下来几篇文章,我们来研读下Sass中的关键功能,今天来看map,大家不妨一坐,精彩内容马上呈现。

map简介

在Sass中,maps代表一种数据类型,可以包含若干键值对的对象类型,使用()包围一个map,里面的键值对用逗号隔开,键和值可以是任何的Sass数据类型,尽管一个值可以用在多个键上,但是通过一个键我们必须只能找到一个值。map不能直接在css中使用,如果你把一个map赋值给一个元素将会报错。下面的代码示例一个经典的map。

$map: (
key1: value1,
key2: value2,
key3: value3
);

map使用

我们可以使用一系列的函数操作map,可以使用循环指令遍历map。

map相关的函数有map-keys()、map-values()、map-get()、map-has-key()、map-merge()、map-remove()、keywords()等,函数功能如下表所示。

函数 功能 示例
map-keys(map) 返回map里面所有的key(list) map-keys(("foo": 1, "bar": 2)) => "foo", "bar"
map-values(map) 返回map里面所有的value(list) map-values(("foo": 1, "bar": 2)) => 1, 2
map-get(map,key) 返回map里面指定可以的value map-get(("foo": 1, "bar": 2), "foo") => 1
map-has-key(map,key) 返回map里面是否含有指定的key map-has-key(("foo": 1, "bar": 2), "foo") => true
map-merge(map1,map2) 合并map(map) map-merge(("foo": 1), ("bar": 2)) => ("foo": 1, "bar": 2)
map-remove(map,keys) 删除指定map中的指定key(map) map-remove(("foo": 1, "bar": 2), "bar") => ("foo": 1)
keywords(args) 返回一个函数参数组成的map(map) @mixin foo(args...){@debug keywords($args); //=> (arg1: val, arg2: val)}

我们可以使用@each指令遍历map,如下所示。

$map: (
key1: value1,
key2: value2,
key3: value3
);
/* 遍历map */
@each $key, $value in $map {
.element--#{$key} {
background: $value;
}
}

map案例

map在Sass中应用广泛,有很多场合可以用到map,下面列举一二。

指定多值

css里有很多属性可以指定多个值,例如transition、box-shadow等,这个时候我们可以使用map来定义不同的值,然后可以统一使用。例如

/* 使用map定义不同的值 */
$card_transition_map: (
trans1: 200ms transform ease-in-out,
trans2: 400ms background ease-in,
trans3: 600ms color linear
);
/* map-values统一使用 */
.card {
transition: map-values($card_transition_map);
}

编译之后输出

.card {
transition: 200ms transform ease-in-out,
400ms background ease-in,
600ms color linear;
}

压缩多值

我们可以使用zip函数来压缩多值,例如操作animate时:

$animation_config: (
name: none,
duration: 0s,
timing: ease,
delay: 0s,
iteration: 1, // infinite
direction: normal, // reverse, alternate, alternate-reverse
fill-mode: none, // forwards, backwards, both
play-state: running
); @function sh-setup($config) {
@return zip(map-values($config)...);
} .object {
animation: sh-setup($animation_config);
}

编译之后输出结果为

.object {
animation: none 0s ease 0s 1 normal none running;
}

指定皮肤

我们可以使用一个循环来遍历不同的map,达到指定不同皮肤的功效。

$background_color: (
jeremy: #0989cb,
beth: #8666ae,
matt: #02bba7,
ryan: #ff8178
);
$font: (
jeremy: Helvetica,
beth: Verdana,
matt: Times,
ryan: Arial
);
@each $key, $value in $background_color {
.#{$key} {
background: $value;
font-family: map-get($font, $key);
}
}

编译之后输出

.jeremy {
background: #0989cb;
font-family: Helvetica;
}
.beth {
background: #8666ae;
font-family: Verdana;
}
.matt {
background: #02bba7;
font-family: Times;
}
.ryan {
background: #ff8178;
font-family: Arial;
}

配置文件

使用Sass的一个最大的优点在于,我们可以对css文件进行统一的组织与管理,使用配置文件是达到目的的主要手段,例如我们把网页中所有层的z-index放配置文件里,在需要的地方进行调用。

/*定义配置文件*/
$z-index: (
modal : 200,
navigation : 100,
footer : 90,
triangle : 60,
navigation-rainbow : 50,
share-type : 41,
share : 40,
);
/*在合适的时机调用*/
.navigation {
z-index: map-get($z-index, navigation);
}

编译之后输出

.navigation {
z-index: 100;
}

上面案例调用的时候还用到map-get函数,还是比较麻烦,我们继续简化。

$z-index: (
modal : 200,
navigation : 100,
footer : 90,
triangle : 60,
navigation-rainbow : 50,
share-type : 41,
share : 40,
);
@function z-index($key) {
@return map-get($z-index, $key);
}
@mixin z-index($key) {
z-index: z-index($key);
}
/*调用时*/
.navigation {
@include z-index(navigation);
}

断点管理

下面代码演示如何在项目管理中如何进行断点管理。

// _config.scss
$breakpoints: (
small: 767px,
medium: 992px,
large: 1200px
); // _mixins.scss
@mixin respond-to($breakpoint) {
@if map-has-key($breakpoints, $breakpoint) {
@media (min-width: #{map-get($breakpoints, $breakpoint)}) {
@content;
}
} @else {
@warn "Unfortunately, no value could be retrieved from `#{$breakpoint}`. "
+ "Please make sure it is defined in `$breakpoints` map.";
}
} // _component.scss
.element {
color: hotpink; @include respond-to(small) {
color: tomato;
}
}

编译之后输出结果为

.element {
color: hotpink;
} @media (min-width: 767px) {
.element {
color: tomato;
}
}

处理前缀

下面我们来看map在处理前缀mixin中的应用,两个参数类型分别为map和list,使用需要注意。

/*定义*/
/// Mixin to prefix several properties at once
/// @author Hugo Giraudel
/// @param {Map} $declarations - Declarations to prefix
/// @param {List} $prefixes (()) - List of prefixes to print
@mixin prefix($declarations, $prefixes: ()) {
@each $property, $value in $declarations {
@each $prefix in $prefixes {
#{'-' + $prefix + '-' + $property}: $value;
} // Output standard non-prefixed declaration
#{$property}: $value;
}
}
/*使用*/
.selector {
@include prefix((
column-count: 3,
column-gap: 1.5em,
column-rule: 2px solid hotpink
), webkit moz);
}

编译之后输出为

.selector {
-webkit-column-count: 3;
-moz-column-count: 3;
column-count: 3;
-webkit-column-gap: 1.5em;
-moz-column-gap: 1.5em;
column-gap: 1.5em;
-webkit-column-rule: 2px solid hotpink;
-moz-column-rule: 2px solid hotpink;
column-rule: 2px solid hotpink;
}

反向函数

Hugo Giraudel大牛定义的反向函数。

/*定义*/
/// Returns the opposite direction of each direction in a list
/// @author Hugo Giraudel
/// @param {List} $directions - List of initial directions
/// @return {List} - List of opposite directions
@function opposite-direction($directions) {
$opposite-directions: ();
$direction-map: (
'top': 'bottom',
'right': 'left',
'bottom': 'top',
'left': 'right',
'center': 'center',
'ltr': 'rtl',
'rtl': 'ltr'
); @each $direction in $directions {
$direction: to-lower-case($direction); @if map-has-key($direction-map, $direction) {
$opposite-directions: append($opposite-directions, unquote(map-get($direction-map, $direction)));
} @else {
@warn "No opposite direction can be found for `#{$direction}`. Direction omitted.";
}
} @return $opposite-directions;
}
/*使用*/
.selector {
background-position: opposite-direction(top right);
}

编译之后输出结果为

.selector {
background-position: bottom left;
}

深入阅读

本文的写作过程大量参考了以下文章,大家可以仔细阅读下面文章获得更深的体会。

声明

前端开发whqet,关注前端开发,分享相关资源。csdn专家博客,王海庆希望能对您有所帮助。

本文原文链接,http://whqet.github.io/2015/02/15/Sass map详解/

*欢迎大家访问CSDN博客,http://blog.csdn.net/whqet/ *

Sass map详解的更多相关文章

  1. List、Set、Map详解及区别

    一.List接口 List是一个继承于Collection的接口,即List是集合中的一种.List是有序的队列,List中的每一个元素都有一个索引:第一个元素的索引值是0,往后的元素的索引值依次+1 ...

  2. 源映射(Source Map)详解

    一.什么是源映射 为了提高性能,很多站点都会先压缩 JavaScript 代码然后上线, 但如果代码运行时出现错误,浏览器只会显示在已压缩的代码中的位置,很难确定真正的源码错误位置. 这时源映射就登场 ...

  3. java中list和map详解

    一.概叙 List , Set, Map都是接口,前两个继承至Collection接口,Map为独立接口, List下有ArrayList,Vector,LinkedList Set下有HashSet ...

  4. Sass中的Map 详解

    Sass中的Map长什么样 Sass 的 map 常常被称为数据地图,也有人称其为数组,因为他总是以 key:value 成对的出现, Sass 的 map 长得与 JSON 极其相似. json: ...

  5. Array.prototype.map()详解

    今天在地铁上看到这样一个小例子: ["1","2","3"].map(parseInt); 相信很多人和我一样,觉得输出的结果是[1,2,3 ...

  6. C++ map详解

    1.什么是mapmap是一个键值对容器.在处理一对一数据是,很有用. 2.map数据结构的特点map内部自建一颗红黑树,这棵树具有对数据自动排序的功能,因此,map内的数据都是按key的值排好序的. ...

  7. python 中的map 详解

    python中的map函数应用于每一个可迭代的项,返回的是一个结果list.如果有其他的可迭代参数传进来,map函数则会把每一个参数都以相应的处理函数进行迭代处理.map()函数接收两个参数,一个是函 ...

  8. 集合框架学习之Collection和Map详解

    线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.本文试图通过简单的描述,向读者阐述各个类的作用以 ...

  9. Javascript中Array.prototype.map()详解

    map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数.callback 每次执行后的返回值组合起来形成一个新数组. callback 函数只会在有值的索引上被调用:那些从来没被赋 ...

随机推荐

  1. js窗体间传值

    A页面传值给 B页面 页面A <html xmlns="http://www.w3.org/1999/xhtml"> <HEAD> <TITLE> ...

  2. 洛谷P1038神经网络题解

    题目 这个题不得不说是一道大坑题,为什么这么说呢,这题目不仅难懂,还非常适合那种被生物奥赛刷下来而来到信息奥赛的学生. 因此我们先分析一下题目的坑点. 1: 题目的图分为输入层,输出层,以及中间层. ...

  3. Visible Trees HDU - 2841

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. ubuntu18.4 中 mysql5.7 全完卸载与安装

    卸载 sudo apt-get autoremove --purge mysql-server-5.7 sudo apt-get remove mysql-server sudo apt-get au ...

  5. windows 服务器同步互联网时间

    @echo off ::netsh ipsec static set policy name=7road assign=n net time /setsntp:pool.ntp.org net sto ...

  6. Python的快排应有的样子

    快排算法 ​ 简单来说就是定一个位置然后,然后把比它小的数放左边,比他大的数放右边,这显然是一个递归的定义,根据这个思路很容易可以写出快排的代码 ​ 快排是我学ACM路上第一个让我记住的代码,印象很深 ...

  7. Centos7之Gcc安装

    1下载Gcc包进行安装https://gcc.gnu.org 下载下来的是一个包文件,但是要进行一系列的编译安装,听麻烦的. 不如直接使用这个yum来从网络上进行安装. 2 yumyum instal ...

  8. LVS+Keepalived搭建高可用负载均衡

    应用环境: LVS负责多台WEB端的负载均衡(LB):Keepalived负责LVS的高可用(HA),这里介绍主备模型. 测试环境: 配置步骤: 1. 安装软件 在LVS-1和LVS-2两台主机上安装 ...

  9. 百度地图API示例:使用vue添加删除覆盖物

    1.index.html <script type="text/javascript" src="http://api.map.baidu.com/api?v=2. ...

  10. 【Asia Yokohama Regional Contest 2018】Arithmetic Progressions

    题目大意:给定 N(1<N<=5000) 个不同元素组成的集合,求从中选出若干数字组成的等差数列最长是多少. 题解:直接暴力有 \(O(n^3)\) 的算法,即:枚举等差数列的前两个值,再 ...