Sass - Check which kind of value a variable has

前端 未结 2 1209
星月不相逢
星月不相逢 2020-12-14 19:49

Suppose i have a variable:

$var: 5px;

but somewhere in code its value have changed in to possible color, number

相关标签:
2条回答
  • 2020-12-14 20:24

    From the Sass documentation:

    type_of($value)

    Returns the type of a value.

    Examples:

    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

    (note that - and _ is interchangeable in Sass functions).

    0 讨论(0)
  • 2020-12-14 20:31

    To be a little clearer, here's how you might use type-of:

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

    In addition to the types shown in the docs, type-of will also return 'map' if passed a SASS map object:

    $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 */
    }
    
    0 讨论(0)
提交回复
热议问题