2014-04-24 37 views
10

假设我有一个变量:萨斯 - 检查一个变量具有何种价值

$var: 5px; 

但在代码中的某处其值已更改为可能颜色EMrm

是否有任何函数来检测它具有哪种类型的值?

@if is-color($var) { //do something } 

我知道有没有IS-色功能的青菜,但有其他的方法来做到这一点还是功能?

回答

18

从萨斯文档:

type_of($值)

返回值的类型。

实例:

type-of(100px) => number 
type-of(asdf) => string 
type-of("asdf") => string 
type-of(true) => bool 
type-of(#fff) => color 
type-of(blue) => color 

http://sass-lang.com/documentation/Sass/Script/Functions.html#type_of-instance_method

(注意-_是可互换的Sass函数)。

+3

也可以是列表,例如:'型的(10px的10px的0 1rem)=> list' – sospedra

+0

@Deerloper好吧,这很好,但我只是引用文档。 – cimmanon

+0

是的,我不说这是坏的,只是添加更多的信息,顺便说一句,我刚刚发现:P – sospedra

3

为了更清晰一点,这里是如何使用类型的:

@if type-of($my-variable) == string { 
    /* do something */ 
} 

除了在文档中显示的类型,如果传递了一个SASS地图类型,也将返回“地图”对象:

$font-sizes: (
    small: rem-calc(18px), 
    medium: rem-calc(20px), 
    large: rem-calc(22px) 
); 

@if type-of($font-sizes) == map { 
    /* do map-related thing */ 
} @else { 
    /* do other thing */ 
}