Imr*_*hio 12 variables if-statement sass
假设我有一个变量:
$var: 5px;
Run Code Online (Sandbox Code Playgroud)
但在代码的某个地方,它的价值已经改变为可能的颜色,数量,em,rm等.
是否有任何功能可以检测它具有哪种类型的值?
即
@if is-color($var) { //do something }
Run Code Online (Sandbox Code Playgroud)
我知道sass中没有is-color函数,但还有其他方法可以做到这一点还是有功能?
cim*_*non 25
从Sass文档:
type_of($值)
返回值的类型.
例子:
Run Code Online (Sandbox Code Playgroud)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函数).
为了更清楚一点,这里是你如何使用type-of:
@if type-of($my-variable) == string {
/* do something */
}
Run Code Online (Sandbox Code Playgroud)
除了文档中显示的类型之外,如果传递了SASS映射对象,type-of也将返回'map':
$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 */
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11735 次 |
最近记录: |