Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate keyframe percentages Less CSS

I have read Less#loops and Less#functions docs. But I can't figure out how to apply percentage function, or a similar way to loop percentages progressively without using such function.

When I calculate it, out of a loop, as pointed out in another post width: percentage(140/620); , it works, but not when trying to loop using variables.

On 2014 @pixelass suggested to use an external library to loop easier, but I don't feel like using an external library.

What I am trying to loop (and doesn't even compile):

.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.
}

I am trying to translate this Sass to Less:

@keyframes anim{ 
    $steps:20; 
    @for $i from 0 through $steps{ 
        #{percentage($i*(1/$steps))}{ 
            // code 
        } 
    } 
}
like image 427
another Avatar asked Jul 15 '16 19:07

another


People also ask

What is percentage in keyframes CSS?

0% is the beginning of the animation, 100% is when the animation is complete. Tip: For best browser support, you should always define both the 0% and the 100% selectors. Note: Use the animation properties to control the appearance of the animation, and also to bind the animation to selectors.

What is @- webkit keyframes in CSS?

The @keyframes rule specifies the animation code. The animation is created by gradually changing from one set of CSS styles to another. During the animation, you can change the set of CSS styles many times.

Can you have multiple keyframes CSS?

You can animate multiple properties inside a keyframe rule and use multiple keyframes to specify an element's property values at specific points in time. For example, suppose you have an element that you want to animate from one position to another, horizontally.

How do I add a delay to a keyframe?

Another technique is to create a new set of @keyframes that is responsible for hiding the animation during the delay. Then, apply that with the original animation, at the same time. A limitation of this technique is that the pause between animations must be an integer multiple of the “paused” keyframes.


1 Answers

It seems like the Less compiler does not evaluate functions when directly used as a selector. Solution would be to make use of a temporary variable like in either of the below snippets:

.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.
}

or

.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.
}
like image 70
Harry Avatar answered Nov 15 '22 12:11

Harry