CSS を使用して色が変化する回転アニメーションの動的な効果を実現する方法

不言
リリース: 2018-08-02 10:58:01
オリジナル
3132 人が閲覧しました

この記事では、CSS を使用して色の変化する回転アニメーションを実現する方法を紹介します。必要な方は参考にしていただければ幸いです。

効果のプレビュー

CSS を使用して色が変化する回転アニメーションの動的な効果を実現する方法

コード解釈

domを定義、コンテナには9つの要素が含まれます:

<div>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
</div>
ログイン後にコピー

中央表示:

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: black;
}
ログイン後にコピー

コンテナのサイズを定義:

.container {
    width: 30em;
    height: 30em;
    font-size: 12px;
}
ログイン後にコピー

の行コンテナ:

.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%;
}
ログイン後にコピー

コンテナ内の線を中心にします:

.container {
    display: flex;
    align-items: center;
    justify-content: center;
}
ログイン後にコピー

線を中心から外側に徐々に伸ばすように変数を定義します:

.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;
}
ログイン後にコピー

線を回転させるアニメーション効果を設定します:

.container span {
    animation: rotating linear infinite;
    animation-duration: calc(5s / (9 - var(--n) + 1));
}

@keyframes rotating {
    to {
        transform: rotate(1turn);
    }
}
ログイン後にコピー

アニメーション効果を定義します色相を使用して色を変更します。360 度の円は 100% で、--percent 変数は 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;
    }
}
ログイン後にコピー

最後に、色の変化するアニメーション効果をコンテナに適用します:

.container {
    --deg: calc(var(--percent) / 100 * 360deg);
    color: hsl(var(--deg), 100%, 50%);
    animation: change-color 5s linear infinite;
}
ログイン後にコピー

You'終わった!

おすすめ関連記事:

CSS と D3 を使用して宇宙船のダイナミックな効果を実現する方法

CSS と D3 を使用して無限の六角形の空間の効果を実現する方法

以上がCSS を使用して色が変化する回転アニメーションの動的な効果を実現する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!