1. 程式人生 > >17.純 CSS 創作炫酷的同心矩形旋轉動畫

17.純 CSS 創作炫酷的同心矩形旋轉動畫

原文地址:https://segmentfault.com/a/1190000014807564

感想: 這個特效不難,但是這想法可能想不到,哈哈,怎麼又廢了。

HTML程式碼:

<div class="loader">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
</div>

CSS程式碼:

html, body ,.loader
{ margin: 0; padding: 0; height: 100%; display: flex; justify-content: center; align-items: center; background-color: black; } /* 設定span的樣式 */ .loader{ position: relative; width: 10em; height: 10em; font-size: 28px; } .loader span{ position: absolute
; width: 100%; height: 100%; /* 0.3表示透明度 */ background-color: rgba(100%, 0%, 0%, 0.3); /* 並排 */ box-sizing: border-box; border: 0.5em solid; /* border-color: 上下 左右 */ border-color: white rgba(100%, 100%, 100%, 0.2); /* 動畫名稱 週期 節奏 迴圈次數 是否反向播放 */ animation: animate 5s ease-in-out infinite alternate
; } @keyframes animate{ 0%{ /* red */ /* 顏色變換 */ background-color: rgba(100%, 0%, 0%, 0.3); /* 旋轉 */ transform: rotate(0deg); } 25% { /* yellow */ background-color: rgba(100%, 100%, 0%, 0.3); } 50% { /* green */ background-color: rgba(0%, 100%, 0%, 0.3); } 75% { /* blue */ background-color: rgba(0%, 0%, 100%, 0.3); } 100% { /* purple */ background-color: rgba(100%, 0%, 100%, 0.3); transform: rotate(720deg); } } /* 分別設定5個span的尺寸 */ .loader span:nth-child(1){ width: calc(20% + 20% * (5-1)); height: calc(20% + 20% * (5-1)); /* 為每個span設定動畫延時 */ animation-delay: calc(0.2s * (5-1)); } .loader span:nth-child(2) { width: calc(20% + 20% * (5 - 2)); height: calc(20% + 20% * (5 - 2)); animation-delay: calc(0.2s * (5 - 2)); } .loader span:nth-child(3) { width: calc(20% + 20% * (5 - 3)); height: calc(20% + 20% * (5 - 3)); animation-delay: calc(0.2s * (5 - 3)); } .loader span:nth-child(4) { width: calc(20% + 20% * (5 - 4)); height: calc(20% + 20% * (5 - 4)); animation-delay: calc(0.2s * (5 - 4)); } .loader span:nth-child(5) { width: calc(20% + 20% * (5 - 5)); height: calc(20% + 20% * (5 - 5)); animation-delay: calc(0.2s * (5 - 5)); }