Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SASS: Give arguments to keyframes

Tags:

css

sass

I did this small glowing animation :

@include keyframes(teal-glowing) {
    0% { background-color: $teal; -webkit-box-shadow: 0 0 3px $teal; }
    50% { background-color: rgba(3,173,172,0.83); -webkit-box-shadow: 0 0 7px rgba(3,173,172,0.83); }
    100% { background-color: $teal; -webkit-box-shadow: 0 0 3px $teal; }
}

I was wondering if it was possible to do a generic one, and just gives it the color I was as argument. Instead of creating a teal-glowing, green-glowing, ...

like image 603
Scipion Avatar asked Dec 13 '16 14:12

Scipion


1 Answers

You can create all the keyframes using a loop, if you can define the colors in a list in the SASS.

This SASS:

$colors: teal, red;
@for $i from 1 through length($colors) {
  @keyframes(#{nth($colors, $i)}-glowing) {
    0% {background-color: nth($colors, $i);}
    50% {background-color: rgba(nth($colors, $i),0.83);}
    100% {background-color: nth($colors, $i);}
  }
}

will compile into this CSS:

@keyframes (teal-glowing) {
  0% {background-color: teal;}
  50% {background-color: rgba(0, 128, 128, 0.83);}
  100% {background-color: teal;}
}
@keyframes (red-glowing) {
  0% {background-color: red;}
  50% {background-color: rgba(255, 0, 0, 0.83);}
  100% {background-color: red;}
}

and then just add as many colors as you want to that list. If You want colors that aren't named HTML colors, you will have to adjust the naming of the keyframes in this loop (possibly adding another SASS variable containing the names).

like image 133
andi Avatar answered Nov 09 '22 15:11

andi