css 如何在SASS中 * 命名 * @关键帧?

vql8enpb  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(86)

我尝试使用SASS中的循环创建以下命名的@keyframes集合

@keyframes loading-1 {
    14.28% {
        opacity: 0.3;
    }
}

@keyframes loading-2 {
    28.57% {
        opacity: 0.3;
    }
}

@keyframes loading-3 {
    42.86% {
        opacity: 0.3;
    }
}

@keyframes loading-4 {
    57.14% {
        opacity: 0.3;
    }
}

@keyframes loading-5 {
    71.43% {
        opacity: 0.3;
    }
}

@keyframes loading-6 {
    85.71% {
        opacity: 0.3;
    }
}

@keyframes loading-7 {
    100% {
        opacity: 0.3;
    }
}

字符串
我一直在看包括StackOverflow在内的几个参考文献,没有一个能解决我试图实现的@keyframe命名类型,我真的不认为有任何重复的帖子能完全解决这个问题。
我希望能够做到这样的事情:

$number-of-steps: 7;

@function to-fixed($float, $digits: 2) {
    $sass-precision: 5;
    
    @if $digits > $sass-precision {
      @warn "Sass sets default precision to #{$sass-precision} digits, and there is no way to change that for now."
      + "The returned number will have #{$sass-precision} digits, even if you asked for `#{$digits}`."
      + "See https://github.com/sass/sass/issues/1122 for further informations.";
    }
    
    $pow: pow(10, $digits);
    @return round($float * $pow) / $pow;
}

   :

@for $l from 1 through $number-of-steps {

    @keyframes loading_#{$l} {
        {to-fixed((100/$l), 2)}% {
            opacity: 0.3;
        }
    }

}


但SASS抱怨说,

Compilation Error
Error: expected "}".
   ╷
18 │     @keyframes loading-#{$l} {
                                   ^


有没有一种方法可以动态地为这些关键帧创建名称?
我正在使用Visual Code Studio中的“Live SASS编译器”,它似乎是最新的。
TIA!

fdx2calv

fdx2calv1#

你的问题是没有动态名称,你有两个错误。第一个是to-fixedinterpolation上缺少#

#{to-fixed((100/$l), 2)}% {
  opacity: 0.3;
}

字符串
第二个是你使用的pow(),这个函数是不可用的,你必须从sass:math导入它:

@use "sass:math";
$pow: math.pow(10, 2);


请注意,pow()仅兼容Dart Dassv1.25.0。

相关问题