今天,有群友问我,使用 CSS 如何完结如下 Loading 后果:
这是一个异常有敬爱的问题。
咱们澄澈,使用 CSS,咱们不错异常圣洁的完结这么一个动画后果:
<div></div>
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); } }
动画如下:
与条件的线条 loading 动画比拟,上述动画贫乏了比较中枢的少量在于:
线条在旋转教训的进程中,瑕瑜是会发生变化的
是以,这里的的难点也就转换为了,如何动态的完结弧形线段的瑕瑜变化?措置了这个问题,也就基本上措置了上述的线条变换 Loading 动画。
本文将先容 CSS 当中,几种有敬爱的,可能不错动态蜕变弧形线条瑕瑜的步地:
步履一:使用遮罩完结第一种步履,亦然比较容易预想的步地,使用遮罩的步地完结。
咱们完结两个半圆线条,一个是骨子能看到的脸色,另外一个则是和背安闲换取的,相对更为粗少量的半圆线条,当两条线条教训的速度不一致时,咱们从视觉上,也就能看到动态变化的弧形线条。
望望显露图,一看就懂:
咱们把上述红色线条,替换成布景白色,举座的动画后果就异常的不异了,伪代码如下:
<div></div>
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); } }
中枢即是完结两条半圆线条,一条玄色,一条背安闲,两段线条以不同的速度教训(通过动画时分及缓动贬抑),后果如下:
无缺的代码你不错猛击 -- CodePen Demo - Linear Loading[1]
上述有假想最大的 2 个问题在于:
若是背安闲不是纯色,会败露
若是条件能展现的线段长度大于半个圆,无法完成
基于此,咱们只可匠心独具。
步履二:借助 SVG 的 stroke-* 智商在之前异常多的篇著作中,都有讲到过在 CSS 合营 SVG,咱们不错完结各式简便或复杂的线条动画,像是简便的:
约略自界说复杂旅途的复杂的线条动画:
> 对 CSS/SVG 完结线条动画感兴致的,然而还不太了解的,不错望望我的这篇著作 -- 【Web动画】SVG 线条动画初学[2]
在这里,咱们只需要一个简便的 SVG 标签 ,合营其 CSS 面容 stroke-dasharray 和 stroke-dashoffset 即可圣洁完成上述后果:
<svg class="circular" viewbox="25 25 50 50"> <circle class="path" cx="50" cy="50" r="20" fill="none" /> </svg>
.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; } }
简便评释下:
stroke:类比 css 中的 border-color,给 svg 图形设定边框脸色; stroke-dasharray:值是一组数组,没数目上限,每个数字轮流显露划线与拒绝的宽度; stroke-dashoffset:dash 模式到旅途开动的距离。咱们诈欺 stroke-dasharray 将蓝本无缺的线条切割成多段,假定是 stroke-dasharray: 10, 10 显露这么一个图形:
第一个 10 显露线段的长度,第二个 10 显露两条可见的线段中间的空闲。
而骨子代码中的 stroke-dasharray: 1, 200,显露在两条 1px 的线段中间,拒绝 200px,由于直径 40px 的圆的周长为 40 * π ≈ 125.6px,小于 200,是以骨子如图下,唯有一个点:
同理,stroke-dasharray: 89, 200 显露:
通过 animation,让线段在这两种景色之间补间变换。而 stroke-dashoffset 的作用则是将线段上前推移,合营父容器的 transform: rotate() 旋动弹画,男女无遮挡猛进猛出免费视频使得视觉后果,线段是在一直在向一个观点旋转。完结如下:
无缺的代码你不错戳这里:CodePen Demo -- Linear loading[3]
OK,还会有同学说了,我不想引入 SVG 标签,我只想使用纯 CSS 有假想。这里,还有一种诈欺 CSS @property 的纯 CSS 有假想。
使用 CSS @property 让 conic-gradient 动起来这里咱们需要借助 CSS @property 的智商,使得本来无法完结动画后果的角向渐变,动起来。
这个步履,我在先容 CSS @property 的著作中也有说起 -- CSS @property,让不成能变可能[4]
深广来说,渐变是无法进活动画后果的,如下所示:
<div></div>
.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%); } }
将会获取这么一种后果,由于 conic-gradient 是不扶植过渡动画的,获取的是一帧向另外一帧的平直变化:
好,使用 CSS @property 自界说变量更动一下:
@property --per { syntax: '<percentage>'; 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%; } }
望望更动后的后果:
在这里,咱们不错让渐变动态的动起来,赋予了动画的智商。
咱们只需要再引入 mask,将中间部分裁切掉,即可完结上述线条 Loading 动画,伪代码如下:
<div></div>
@property --per { syntax: "<percentage>"; 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(),让线条在旋转的同期,脸色也随着变化,最终后果如下,这是一个纯 CSS 措置有假想:
无缺的代码你不错猛击这里:Linear Loading Animation[5]
本有假想的独一问题在于,现时 CSS @property 的兼容性稍稍不是那么乐观。虽然,异日可期。
临了简便回来一下,本文先容了 3 种完结动态弧形线条瑕瑜变化的 Loading 动画,虽然,它们各有优劣,骨子使用的时刻笔据骨子情况具体采选。
有的时刻,切图也许亦然更省时分的一种步地。
好了,本文到此完结,但愿本文对你有所匡助 :)
若是还有什么疑问约略提倡,不错多多交流,原创著作,文笔有限,目不识字,文中若有不正之处,万望示知。
参考贵府[1]CodePen Demo - Linear Loading:
https://codepen.io/Chokcoco/pen/PvqYNJ
[2]【Web动画】SVG 线条动画初学:
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