This article introduces to you about how to use CSS to realize the dynamic effect of color rotation animation, there is a certain reference value, there is a need for friends to refer to, I hope you have some help.
Effect Preview
Code interpretation
To define the DOM, the container contains 9 elements:
<div class= "Container" > <span></span> <span></span> <span></ span> <span></span> <span></span> <span></span> < span></span> <span></span> <span></span></div>
Center display:
body { margin:0; HEIGHT:100VH; Display:flex; Align-items:center; Justify-content:center; Background-color:black;}
Define Container Dimensions:
. container { width:30em; Height:30em; font-size:12px;}
To set the style of a line in a container:
. container { color:lime;}. Container span { position:absolute; width:5em; height:5em; Border-style:solid; Border-width:1em 1em 0 0; Border-color:currentcolor Transparent; border-radius:50%;}
To center a line in a container:
. container { Display:flex; Align-items:center; Justify-content:center;}
Define variables to gradually extend the lines from the center outward:
. container span { --diameter:calc (5em + (VAR (--n)-1) * 3em); Width:var (--diameter); Height:var (--diameter);}. Container Span:nth-child (1) { --n:1;}. Container Span:nth-child (2) { --n:2;}. Container Span:nth-child (3) { --n:3;}. Container Span:nth-child (4) { --n:4;}. Container Span:nth-child (5) { --n:5;}. Container Span:nth-child (6) { --n:6;}. Container Span:nth-child (7) { --n:7;}. Container Span:nth-child (8) { --n:8;}. Container Span:nth-child (9) { --n:9;}
Set the animation effect to rotate the line:
. container span { animation:rotating linear infinite; Animation-duration:calc (5s/(9-var (--n) + 1));} @keyframes Rotating {To { transform:rotate (1turn); }}
Defines the animation effect that changes color, with the Hue ring 360 degrees a week to the 100%,--percent variable is where this 100% is located:
@keyframes Change-color { 0, 100% { --percent:0; } 10% { --percent:10; } 20% { --percent:20; } 30% { --percent:30; } 40% { --percent:40; } 50% { --percent:50; } 60% { --percent:60; } 70% { --percent:70; } 80% { --percent:80; } 90% { --percent:90; }}
Finally, apply the animated effect of color change to the container:
. container { --deg:calc (VAR (--percent)/360deg); COLOR:HSL (Var (--deg), 100%, 50%); Animation:change-color 5s linear Infinite;}
Done!
Related articles recommended:
How to realize the dynamic effect of spaceship with CSS and D3
How to use CSS and D3 to achieve the effects of endless hexagon space