Search code examples
lesscss-animationsmixinsless-mixins

LESS: mixin to create Blink animation, passing @color -stops


I wish to create a CSS animation that act like "Blink effect" using LESS. My purpose is to call a single mixin passing each time 2 @stop colors in order to get diffent color blink depending by css class of DOM element.

Currently I have the following HTML:

  <p class='blink'>
    Loading...
  </p>

  <p class='blink alert'>
    <big>WARNING!!!! Operation failed.</big>
  </p>

And here, LESS CODE:

.blink
{
  .animation-blink-mixin(@dark-green,@light-green);

  &.alert
  {
    .animation-blink-mixin(@dark-red,@light-red);
  }
}

ANIMATION MIXIN:

.animation-blink-mixin (@stop1, @stop2, @time:2s)
{
  animation:animation-blink @durata infinite;

  .steps()
  {
      0% { color:@stop1; }
     50% { color:@stop2; }
    100% { color:@stop1; }
  }

  @keyframes animation-blink { .steps(); }
}

My problem is that both DOM elements have the same "red color" animation, instead being one green2green and others red2red.

I understood that problem is in "animation name" that is always the same. Some suggestion to reach desired behaviour?

Thanks.


Solution

  • Just set the animation name explicitly, e.g (codepen demo):

    .blink {
        .animation-blink(blink, #080 + 200, #080);
        &.alert {
            .animation-blink(alert, #800, #800 + 200);
        }
    }
    
    .animation-blink(@name_, @color1, @color2, @time: .5s) {
    
        @name: ~"animation-blink-@{name_}";
        animation: @name @time ease-in-out infinite alternate;
    
        @keyframes @name {
            0% {color: @color1}
            to {color: @color2}
        }
    }