This can be done using pure CSS without writing (or generating via SCSS, etc.) using a combination of
- Negative
animation-delay to change animation start time - A few
nth-child or nth-of-type rules for applying formulas that will βrandomizeβ a rule.
movie.nth-child(2n) { animation-delay: -10s } movie.nth-child(2n+1) { animation-delay: -30s } movie.nth-child(3n) { animation-delay: -20s; } movie.nth-child(5n) { animation-delay: -40s } movie.nth-child(7n) { animation-delay: -15s } {etc}
Using only the first 2 rules gives alternating rules (for example, even / odd rows in a table). Pay attention to the second rule, which has an offset of +1 - this is important if your class ( movie ) does not have the corresponding default value for the rule being changed (0 by default, anyways for animation-delay ).
Using nth-child(n) formulas with integer multiple values ββof n makes the effective length of the pattern equal to the product of all your simple factors (for example, 2*3*5*7 = 210 elements before repeating).
li { animation: movie 5s linear infinite; } @keyframes movie { 20% { color: white } 40% { color: black } } li:nth-child(2n-1) { background-color: lime; animation-delay: 1s; } li:nth-child(2n) { background-color: orange; animation-delay: 2s; } li:nth-child(3n) { background-color: yellow; animation-delay: 3s; } li:nth-child(5n) { background-color: magenta; animation-delay: 5s; } li:nth-child(7n) { background-color: aqua; }
<ul> <li>0</li> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>11</li> <li>12</li> <li>13</li> </ul>
For further randomization, you can create a second set of rules with slightly different n multiple / offsets and change the animation-duration (or any other rule).
source share