Home > Web Front-end > CSS Tutorial > How to use CSS to achieve dynamic effects of color-changing rotation animation

How to use CSS to achieve dynamic effects of color-changing rotation animation

不言
Release: 2018-08-02 10:58:01
Original
3249 people have browsed it

This article introduces to you how to use CSS to achieve the dynamic effect of color-changing rotation animation. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Effect preview

How to use CSS to achieve dynamic effects of color-changing rotation animation

Code interpretation

Definition of dom, container Contains 9 elements:

<div>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
</div>
Copy after login

Centered display:

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: black;
}
Copy after login

Define the container size:

.container {
    width: 30em;
    height: 30em;
    font-size: 12px;
}
Copy after login

Set the style of the lines in the 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%;
}
Copy after login

Let the lines Display in the center of the container:

.container {
    display: flex;
    align-items: center;
    justify-content: center;
}
Copy after login

Define variables to gradually extend the line from the center to the outside:

.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;
}
Copy after login

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);
    }
}
Copy after login

Define the color change The animation effect is based on 360 degrees of the hue circle as 100%. The --percent variable refers to the position of 100%:

@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;
    }
}
Copy after login

Finally, apply the color-changing animation effect to the container:

.container {
    --deg: calc(var(--percent) / 100 * 360deg);
    color: hsl(var(--deg), 100%, 50%);
    animation: change-color 5s linear infinite;
}
Copy after login

Done!

Recommended related articles:

How to use CSS and D3 to achieve the dynamic effect of a spaceship

How to use CSS and D3 to achieve endless six The effect of edge space

The above is the detailed content of How to use CSS to achieve dynamic effects of color-changing rotation animation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template