Robert Crocker

Craft obsessed developer who designs.

← Back to the lab

36Dynamic Keyframes

III · Particles & Play

Oscillation tuned per ball with CSS variables; the button freezes them mid-flight.

Click

Source

3 files
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CodeSandbox</title>
  </head>
  <body>
    <style>
      @keyframes oscillate {
        from {
          transform: translateX(calc(var(--amount) * -1));
        }
        to {
          transform: translateX(var(--amount));
        }
      }
    
      .ball {
        animation: oscillate 700ms ease-in-out alternate infinite;
      }
    </style>
    
    <!-- Edit these values to change the oscillation amount: -->
    <div class="ball" style="--amount: 8px"></div>
    <div class="ball" style="--amount: 16px"></div>
    <div class="ball" style="--amount: 32px"></div>
    
    <button>Play/pause animation</button>
  </body>
</html>