Use different @keyframes based on parent element class

后端 未结 1 983
深忆病人
深忆病人 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;
      }
    }
    <div class="box light">
      Class defined on the element
    </div>
    <div class="dark">
      <div class="box">
        Class defined on a parent element
      </div>
    </div>

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