前言
開發(fā)項(xiàng)目遇到需要添加前端loading樣式的問題,遂想到參考一些成熟的解決方案。找到了UIverse網(wǎng)站Explore 3000+ Free UI Elements: CSS & Tailwind (uiverse.io)這個(gè)樣式庫,動(dòng)手學(xué)習(xí)了幾個(gè)常用的loading樣式并進(jìn)行了修改學(xué)習(xí)。 下面先放上各樣式對(duì)應(yīng)的html和css片段(均為MIT許可)。
樣式預(yù)覽及代碼片段
<div class="loader"></div>
.loader {
display: block;
--height-of-loader: 4px;
--loader-color: #0071e2;
width: 130px;
height: var(--height-of-loader);
border-radius: 30px;
background-color: rgba(0,0,0,0.2);
position: relative;
}
.loader::before {
content: "";
position: absolute;
background: var(--loader-color);
top: 0;
left: 0;
width: 0%;
height: 100%;
border-radius: 30px;
animation: moving 1s ease-in-out infinite;
;
}
@keyframes moving {
50% {
width: 100%;
}
100% {
width: 0;
right: 0;
left: unset;
}
}
<div class="loading">
<svg width="64px" height="48px">
<polyline points="0.157 23.954, 14 23.954, 21.843 48, 43 0, 50 24, 64 24" id="back"></polyline>
<polyline points="0.157 23.954, 14 23.954, 21.843 48, 43 0, 50 24, 64 24" id="front"></polyline>
</svg>
</div>
.loading svg polyline {
fill: none;
stroke-width: 3;
stroke-linecap: round;
stroke-linejoin: round;
}
.loading svg polyline#back {
fill: none;
stroke: #ff4d5033;
}
.loading svg polyline#front {
fill: none;
stroke: #ff4d4f;
stroke-dasharray: 48, 144;
stroke-dashoffset: 192;
animation: dash_682 1.4s linear infinite;
}
@keyframes dash_682 {
72.5% {
opacity: 0;
}
to {
stroke-dashoffset: 0;
}
}
3. Loader by G4b413l
<div class="three-body">
<div class="three-body__dot"></div>
<div class="three-body__dot"></div>
<div class="three-body__dot"></div>
</div>
.three-body {
--uib-size: 35px;
--uib-speed: 0.8s;
--uib-color: #5D3FD3;
position: relative;
display: inline-block;
height: var(--uib-size);
width: var(--uib-size);
animation: spin78236 calc(var(--uib-speed) * 2.5) infinite linear;
}
.three-body__dot {
position: absolute;
height: 100%;
width: 30%;
}
.three-body__dot:after {
content: '';
position: absolute;
height: 0%;
width: 100%;
padding-bottom: 100%;
background-color: var(--uib-color);
border-radius: 50%;
}
.three-body__dot:nth-child(1) {
bottom: 5%;
left: 0;
transform: rotate(60deg);
transform-origin: 50% 85%;
}
.three-body__dot:nth-child(1)::after {
bottom: 0;
left: 0;
animation: wobble1 var(--uib-speed) infinite ease-in-out;
animation-delay: calc(var(--uib-speed) * -0.3);
}
.three-body__dot:nth-child(2) {
bottom: 5%;
right: 0;
transform: rotate(-60deg);
transform-origin: 50% 85%;
}
.three-body__dot:nth-child(2)::after {
bottom: 0;
left: 0;
animation: wobble1 var(--uib-speed) infinite
calc(var(--uib-speed) * -0.15) ease-in-out;
}
.three-body__dot:nth-child(3) {
bottom: -5%;
left: 0;
transform: translateX(116.666%);
}
.three-body__dot:nth-child(3)::after {
top: 0;
left: 0;
animation: wobble2 var(--uib-speed) infinite ease-in-out;
}
@keyframes spin78236 {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
@keyframes wobble1 {
0%,
100% {
transform: translateY(0%) scale(1);
opacity: 1;
}
50% {
transform: translateY(-66%) scale(0.65);
opacity: 0.8;
}
}
@keyframes wobble2 {
0%,
100% {
transform: translateY(0%) scale(1);
opacity: 1;
}
50% {
transform: translateY(66%) scale(0.65);
opacity: 0.8;
}
}
<svg viewBox="25 25 50 50">
<circle r="20" cy="50" cx="50"></circle>
</svg>
svg {
width: 3.25em;
transform-origin: center;
animation: rotate4 2s linear infinite;
}
circle {
fill: none;
stroke: hsl(214, 97%, 59%);
stroke-width: 2;
stroke-dasharray: 1, 200;
stroke-dashoffset: 0;
stroke-linecap: round;
animation: dash4 1.5s ease-in-out infinite;
}
@keyframes rotate4 {
100% {
transform: rotate(360deg);
}
}
@keyframes dash4 {
0% {
stroke-dasharray: 1, 200;
stroke-dashoffset: 0;
}
50% {
stroke-dasharray: 90, 200;
stroke-dashoffset: -35px;
}
100% {
stroke-dashoffset: -125px;
}
}
<div class="relative flex w-64 animate-pulse gap-2 p-4">
<div class="h-12 w-12 rounded-full bg-slate-400"></div>
<div class="flex-1">
<div class="mb-1 h-5 w-3/5 rounded-lg bg-slate-400 text-lg"></div>
<div class="h-5 w-[90%] rounded-lg bg-slate-400 text-sm"></div>
</div>
<div class="absolute bottom-5 right-0 h-4 w-4 rounded-full bg-slate-400"></div>
</div>
作者:Zeworl_rod
鏈接:https://juejin.cn/post/7364404706918744105
來源:稀土掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
該文章在 2024/5/6 15:04:41 編輯過