前端每日實戰:27# 視頻演示如何用純 CSS 創做一個精彩的彩虹 loading 特效

圖片描述

效果預覽

按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css

https://codepen.io/comehope/pen/vjvoowhtml

可交互視頻教程

此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端

請用 chrome, safari, edge 打開觀看。git

https://scrimba.com/p/pEgDAM/cPLGLhVgithub

源代碼下載

每日前端實戰系列的所有源代碼請從 github 下載:spring

https://github.com/comehope/front-end-daily-challengeschrome

代碼解讀

定義 dom:dom

<div class="rainbow">
    <div class="bows">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
</div>

居中顯示:flex

html, body, .bows {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: black;
}

定義彩虹的尺寸:動畫

.rainbow {
    width: 20em;
    height: 10em;
}

定義彩虹內拱形的尺寸:

.bows {
    width: 100%;
    height: 200%;
    position: relative;
}

定義彩虹內全部拱形共有的特性:

.bows {
    transform: rotate(225deg);
}

.bows span {
    position: absolute;
    width: calc(100% - 2em * (var(--n) - 1));
    height: calc(100% - 2em * (var(--n) - 1));
    border: 1em solid var(--color);
    box-sizing: border-box;
    border-top-color: transparent;
    border-left-color: transparent;
    border-radius: 50%;
}

分別設置每一個拱形的個性變量:

.bows span:nth-child(1) {
    --n: 1;
    --color: orangered;
}

.bows span:nth-child(2) {
    --n: 2;
    --color: orange;
}

.bows span:nth-child(3) {
  --n: 3;
  --color: yellow;
}

.bows span:nth-child(4) {
  --n: 4;
  --color: mediumspringgreen;
}

.bows span:nth-child(5) {
  --n: 5;
  --color: deepskyblue;
}

.bows span:nth-child(6) {
  --n: 6;
  --color: mediumpurple;
}

定義動畫效果:

.bows span {
    animation: rotating 3s infinite;
    animation-delay: calc(0.05s * var(--n));
}

@keyframes rotating {
    0%, 20% {
        transform: rotate(0deg);
    }

    80%, 100% {
        transform: rotate(360deg);
    }
}

最後,隱藏掉容器以外的內容:

.rainbow {
    overflow: hidden;
}

大功告成!

相關文章
相關標籤/搜索