Use different @keyframes based on parent element class

后端 未结 1 984
深忆病人
深忆病人 2021-01-19 17:34

I have an animation like below:

@keyframes pulseColorGreen {
    0% {
        background-color: green;
    }
    97%{
        background-color: green;
    }
         


        
1条回答
  •  后悔当初
    2021-01-19 17:51

    You can use CSS variable and you will need only one keyframes that you don't have to change. Simply change the main class that define the color:

    .light {
      --c: yellow;
    }
    
    .dark {
      --c: darkblue;
    }
    
    .box {
      height: 100px;
      margin: 5px;
      box-shadow: 0 0 5px var(--c);
      color:#fff;
      animation: pulseColorGreenMkt 1s infinite linear alternate;
    }
    
    @keyframes pulseColorGreenMkt {
      0% {
        background-color: var(--c);
      }
      100% {
        background-color: black;
      }
    }
    Class defined on the element
    Class defined on a parent element

    0 讨论(0)
提交回复
热议问题