【问题标题】:How to iterate keyframe percentages Less CSS如何迭代关键帧百分比 Less CSS
【发布时间】:2016-11-19 03:24:08
【问题描述】:

我已阅读 Less#loopsLess#functions 文档。 但我不知道如何应用percentage 函数,或类似的方法来逐步循环百分比而不使用此类函数。

当我在另一个循环中计算它时,正如另一个 post width: percentage(140/620); 中指出的那样,它可以工作,但在尝试使用变量循环时却不行。

在 2014 年,@pixelass 建议使用外部 library 来更轻松地循环,但我不想使用外部库。

我要循环的内容(甚至不编译):

.loop (@n, @index: 0) when (@index < @n) {
     percentage(@index * (100/@n)){ // This line is messing up my day.
         // code
     }
     .loop(@n, (@index + 1)); // Next iteration.
}
@keyframes anim {
    .loop(20); // Launch the loop.
}

我正在尝试将这个 Sass 翻译成 Less:

@keyframes anim{ 
    $steps:20; 
    @for $i from 0 through $steps{ 
        #{percentage($i*(1/$steps))}{ 
            // code 
        } 
    } 
}

【问题讨论】:

  • 我正在尝试将此 SASS 翻译为 LESS:@keyframes anim{ $steps:20; @for $i from 0 through $steps{ #{percentage($i*(1/$steps))}{ // code } } }

标签: css loops animation less css-animations


【解决方案1】:

当直接用作选择器时,Less 编译器似乎不会评估函数。解决方案是使用以下任一 sn-ps 中的临时变量:

.loop (@n, @index: 0) when (@index <= @n) { /* note the <= as we need 100% frame also */
  @keyframeSel: percentage(@index/@n); /* note the lack of * 100 as Less already does it */
  @{keyframeSel}{
    prop: value;
  }
  .loop(@n, (@index + 1)); // Next iteration.
}
@keyframes anim {
  .loop(20); // Launch the loop.
}

.loop (@n, @index: 0) when (@index <= @n) {
  @keyframeSel: @index/@n * 100%;
  @{keyframeSel}{
    prop: value;
  }
  .loop(@n, (@index + 1)); // Next iteration.
}
@keyframes anim {
  .loop(20); // Launch the loop.
}

【讨论】:

  • 谢谢@Harry。它工作正常,还可以帮助我使用更多我计划使用的 Less 函数,现在有了你的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 2015-04-06
相关资源
最近更新 更多