最近,群里有個(gè)很有意思的問題,使用 CSS 如何實(shí)現(xiàn)如下 Loading 效果:
(資料圖)
這是一個(gè)非常有意思的問題。
我們知道,使用 CSS,我們可以非常輕松的實(shí)現(xiàn)這樣一個(gè)動(dòng)畫效果:
div { width: 100px; height: 100px; border-radius: 50%; border: 2px solid transparent; border-top: 2px solid #000; border-left: 2px solid #000; animation: rotate 3s infinite linear;}@keyframes rotate { 100% { transform: rotate(360deg); }}
動(dòng)畫如下:
與要求的線條 loading 動(dòng)畫相比,上述動(dòng)畫缺少了比較核心的一點(diǎn)在于:
線條在旋轉(zhuǎn)運(yùn)動(dòng)的過程中,長(zhǎng)短是會(huì)發(fā)生變化的所以,這里的的難點(diǎn)也就轉(zhuǎn)變?yōu)榱耍绾蝿?dòng)態(tài)的實(shí)現(xiàn)弧形線段的長(zhǎng)短變化?解決了這個(gè)問題,也就基本上解決了上述的線條變換 Loading 動(dòng)畫。
本文將介紹 CSS 當(dāng)中,幾種有意思的,可能可以動(dòng)態(tài)改變弧形線條長(zhǎng)短的方式:
方法一:使用遮罩實(shí)現(xiàn)第一種方法,也是比較容易想到的方式,使用遮罩的方式實(shí)現(xiàn)。
我們實(shí)現(xiàn)兩個(gè)半圓線條,一個(gè)是實(shí)際能看到的顏色,另外一個(gè)則是和背景色相同的,相對(duì)更為粗一點(diǎn)的半圓線條,當(dāng)兩條線條運(yùn)動(dòng)的速率不一致時(shí),我們從視覺上,也就能看到動(dòng)態(tài)變化的弧形線條。
看看示意圖,一看就懂:
我們把上述紅色線條,替換成背景白色,整體的動(dòng)畫效果就非常的相似了,偽代碼如下:
div { width: 200px; height: 200px;}div::before { position: absolute; content: ""; top: 0px; left: 0px; right: 0px; bottom: 0px; border-radius: 50%; border: 3px solid transparent; border-top: 3px solid #000; border-left: 3px solid #000; animation: rotate 3s infinite ease-out;}div::after { position: absolute; content: ""; top: -2px; left: -2px; right: -2px; bottom: -2px; border-radius: 50%; border: 7px solid transparent; border-bottom: 7px solid #fff; border-right: 7px solid #fff; animation: rotate 4s infinite ease-in-out;}@keyframes rotate { 100% { transform: rotate(0deg); }}
核心就是實(shí)現(xiàn)兩條半圓線條,一條黑色,一條背景色,兩段線條以不同的速率運(yùn)動(dòng)(通過動(dòng)畫時(shí)間及緩動(dòng)控制),效果如下:
完整的代碼你可以猛擊 --CodePen Demo - Linear Loading[1]
上述方案最大的 2 個(gè)問題在于:
如果背景色不是純色,會(huì)露餡。如果要求能展現(xiàn)的線段長(zhǎng)度大于半個(gè)圓,無法完成。基于此,我們只能另辟蹊徑。
方法二:借助 SVG 的 stroke-* 能力在之前非常多的篇文章中,都有講到過在 CSS 配合 SVG,我們可以實(shí)現(xiàn)各種簡(jiǎn)單或復(fù)雜的線條動(dòng)畫,像是簡(jiǎn)單的:
或者自定義復(fù)雜路徑的復(fù)雜的線條動(dòng)畫:
> 對(duì) CSS/SVG 實(shí)現(xiàn)線條動(dòng)畫感興趣的,但是還不太了解的,可以看看我的這篇文章 --【W(wǎng)eb動(dòng)畫】SVG 線條動(dòng)畫入門[2]
在這里,我們只需要一個(gè)簡(jiǎn)單的 SVG 標(biāo)簽
.circular { width: 100px; height: 100px; animation: rotate 2s linear infinite;}.path { stroke-dasharray: 1, 200; stroke-dashoffset: 0; stroke: #000; animation: dash 1.5s ease-in-out infinite}@keyframes rotate { 100% { transform: rotate(360deg); }}@keyframes dash { 0% { stroke-dasharray: 1, 200; stroke-dashoffset: 0; } 50% { stroke-dasharray: 89, 200; stroke-dashoffset: -35px; } 100% { stroke-dasharray: 89, 200; stroke-dashoffset: -124px; }}
簡(jiǎn)單解釋下:
stroke:類比 css 中的 border-color,給 svg 圖形設(shè)定邊框顏色。stroke-dasharray:值是一組數(shù)組,沒數(shù)量上限,每個(gè)數(shù)字交替表示劃線與間隔的寬度。stroke-dashoffset:dash 模式到路徑開始的距離。我們利用stroke-dasharray將原本完整的線條切割成多段,假設(shè)是stroke-dasharray: 10, 10表示這樣一個(gè)圖形:
第一個(gè) 10 表示線段的長(zhǎng)度,第二個(gè) 10 表示兩條可見的線段中間的空隙。
而實(shí)際代碼中的stroke-dasharray: 1, 200,表示在兩條1px的線段中間,間隔200px,由于直徑40px的圓的周長(zhǎng)為40 * π ≈ 125.6px,小于200,所以實(shí)際如圖下,只有一個(gè)點(diǎn):
同理,stroke-dasharray: 89, 200表示:
通過 animation,讓線段在這兩種狀態(tài)之間補(bǔ)間變換。而stroke-dashoffset的作用則是將線段向前推移,配合父容器的transform: rotate()旋轉(zhuǎn)動(dòng)畫,使得視覺效果,線段是在一直在向一個(gè)方向旋轉(zhuǎn)。結(jié)果如下:
完整的代碼你可以戳這里:CodePen Demo -- Linear loading[3]
OK,還會(huì)有同學(xué)說了,我不想引入 SVG 標(biāo)簽,我只想使用純 CSS 方案。這里,還有一種利用 CSS @property 的純 CSS 方案。
使用 CSS @property 讓 conic-gradient 動(dòng)起來這里我們需要借助 CSS @property 的能力,使得本來無法實(shí)現(xiàn)動(dòng)畫效果的角向漸變,動(dòng)起來。
這個(gè)方法,我在介紹 CSS @property 的文章中也有提及 --CSS @property,讓不可能變可能[4]
正常來說,漸變是無法進(jìn)行動(dòng)畫效果的,如下所示:
.normal { width: 200px; height: 200px; border-radius: 50%; background: conic-gradient(yellowgreen, yellowgreen 25%, transparent 25%, transparent 100%); transition: background 300ms; &:hover { background: conic-gradient(yellowgreen, yellowgreen 60%, transparent 60.1%, transparent 100%); }}
將會(huì)得到這樣一種效果,由于conic-gradient是不支持過渡動(dòng)畫的,得到的是一幀向另外一幀的直接變化:
好,使用 CSS @property 自定義變量改造一下:
@property --per { syntax: ""; inherits: false; initial-value: 25%;}div { background: conic-gradient(yellowgreen, yellowgreen var(--per), transparent var(--per), transparent 100%); transition: --per 300ms linear; &:hover { --per: 60%; }}
看看改造后的效果:
在這里,我們可以讓漸變動(dòng)態(tài)的動(dòng)起來,賦予了動(dòng)畫的能力。
我們只需要再引入 mask,將中間部分裁切掉,即可實(shí)現(xiàn)上述線條 Loading 動(dòng)畫,偽代碼如下:
@property --per { syntax: ""; inherits: false; initial-value: 10%;}div { position: relative; width: 100px; height: 100px; border-radius: 50%; animation: rotate 11s infinite ease-in-out; &::before { content: ""; position: absolute; top: 0; left: 0; right: 0; bottom: 0; border-radius: 50%; background: conic-gradient(transparent, transparent var(--per), #fa7 var(--per), #fa7); mask: radial-gradient(transparent, transparent 47.5px, #000 48px, #000); animation: change 3s infinite cubic-bezier(0.57, 0.29, 0.49, 0.76); }}@keyframes change { 50% { transform: rotate(270deg); --per: 98%; } 100% { transform: rotate(720deg); }}@keyframes rotate { 100% { transform: rotate(360deg); filter: hue-rotate(360deg); }}
這里,我順便加上了filter: hue-rotate(),讓線條在旋轉(zhuǎn)的同時(shí),顏色也跟著變化,最終效果如下,這是一個(gè)純 CSS 解決方案:
完整的代碼你可以猛擊這里:Linear Loading Animation[5]
本方案的唯一問題在于,當(dāng)前 CSS @property 的兼容性稍微不是那么樂觀。當(dāng)然,未來可期。
最后簡(jiǎn)單總結(jié)一下,本文介紹了 3 種實(shí)現(xiàn)動(dòng)態(tài)弧形線條長(zhǎng)短變化的 Loading 動(dòng)畫,當(dāng)然,它們各有優(yōu)劣,實(shí)際使用的時(shí)候根據(jù)實(shí)際情況具體取舍。
有的時(shí)候,切圖也許也是更省時(shí)間的一種方式。
好了,本文到此結(jié)束,希望本文對(duì)你有所幫助 :)
參考資料[1]CodePen Demo - Linear Loading:https://codepen.io/Chokcoco/pen/PvqYNJ。
[2]【W(wǎng)eb動(dòng)畫】SVG 線條動(dòng)畫入門:https://www.cnblogs.com/coco1s/p/6225973.html。
[3]CodePen Demo -- Linear loading:https://codepen.io/Chokcoco/pen/jOGQGJP?editors=1100。
[4]CSS @property,讓不可能變可能:https://github.com/chokcoco/iCSS/issues/109。
[5]Linear Loading Animation:https://codepen.io/Chokcoco/pen/ZEXmJxP?editors=1100。
[6]Github -- iCSS:https://github.com/chokcoco/iCSS。
關(guān)鍵詞: