Sass 带十进制数的控制指令

Sass control directive with decimal numbers

我正在尝试让 sass 函数运行:

$times: 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.75 0.80 0.90;

@each $value in $times{
  .is-hidden-#{$value}{
    @include hide( $value + s );
  }
}

问题是:不知何故,它不接受十进制数。它与 1、2、3 等完美配合。我什至试过这个:

$times: 10 20 30 40 50 60 70 80 90;

@each $value in $times{
  .is-hidden-#{$value}{
    @include hide( ($value / 100) + s );
  }
}

但是 sass 对那里的最后一个计算没有做任何事情。他只是忽略了'/ 100'。有什么方法可以通过这样的函数传递十进制数吗?输出将是这样的:

.is-hidden-0.1s{

}

.is-hidden-0.2s{

}

.is-hidden-0.3s{

}

etc....

谢谢!

--- 编辑 --- 这是隐藏混入:

@mixin hide( $time ){
  opacity: 0;
  visibility: hidden;
  @include transition( $time );
}

您想要的输出无效CSS。来自 CSS 验证器:

In CSS1, a class name could start with a digit (".55ft"), unless it was a dimension (".55in"). In CSS2, such classes are parsed as unknown dimensions (to allow for future additions of new units) To make "1s" a valid class, CSS2 requires the first digit to be escaped ".s" [1s]

当您尝试生成无效的 CSS 时,很多时候 Sass 会给您错误。要生成有效结果,您需要使用整数组成选择器并添加转义小数点,如下所示:

@for $i from 1 through 9 {
  .is-hidden-0\.#{$i}s {
    @include hide($i / 10 * 1s);
  }
}

这是对 class 名称的十进制数字进行转义的通用方法(将 . 替换为 -)

@function strip-units ( $number ) {
    @return $number / ( $number * 0 + 1 );
}

@function escape-number ( $value ) {
    @if type-of( $value ) != 'number' {
        @return $value;
    }
    $int: floor( strip-units( $value ) );
    $fract: $value - $int;
    @if ( $fract == 0 ) {
        @return $int;
    }
    @while ( $fract != floor( $fract ) ) {
        $fract: $fract * 10;
    }
    @return $int + '-' + $fract;
}