我用 CSS3 實現了一個超炫的 3D 載入動畫

来源:https://www.cnblogs.com/frontworld/archive/2022/04/18/16159131.html
-Advertisement-
Play Games

本文將介紹利用 CSS 實現滾動視差效果的一個小技巧,並且,利用這個技巧來製作一些有意思的交互特效。 關於使用 CSS 實現滾動視差效果,在之前有一篇文章詳細描述過具體方案 - CSS 實現視差效果,感興趣的同學可以先看看這篇文章。 這裡,會運用上這樣一種純 CSS 的視差技巧: 使用 transf ...


今天給大家帶來一個非常炫酷的CSS3載入Loading動畫,它的特別之處在於,整個Loading動畫呈現出了3D的視覺效果。這個Loading載入動畫由12個3D圓柱體圍成一個橢圓形,同時這12個圓柱體依次波浪式地起伏,從而傳遞給用戶“正在載入”的信息。

效果預覽

image

代碼實現

HTML代碼

HTML代碼十分簡單,一共有三類對象,一個是最外面的橢圓容器,一個是12個圓柱體,最後是“Loading”文本。

<div class="pl">
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__dot"></div>
	<div class="pl__text">Loading…</div>
</div>

CSS代碼

CSS代碼主要做三件事情:

首先是讓最外面的橢圓容器呈現出內陷的3D視覺效果,這裡用帶了box-shadow屬性:

.pl {
  box-shadow: 2em 0 2em rgba(0, 0, 0, 0.2) inset, -2em 0 2em rgba(255, 255, 255, 0.1) inset;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  transform: rotateX(30deg) rotateZ(45deg);
  width: 15em;
  height: 15em;
}

然後是讓12個<div>元素成為3D效果的小圓圈,這裡主要用到了box-shadowborder-radius屬性:

.pl__dot {
  border-radius: 50%;
}
.pl__dot {
  animation-name: shadow;
  box-shadow: 0.1em 0.1em 0 0.1em black, 0.3em 0 0.3em rgba(0, 0, 0, 0.5);
  top: calc(50% - 0.75em);
  left: calc(50% - 0.75em);
  width: 1.5em;
  height: 1.5em;
}
.pl__dot, .pl__dot:before, .pl__dot:after {
  animation-duration: 2s;
  animation-iteration-count: infinite;
  position: absolute;
}
.pl__dot:before, .pl__dot:after {
  content: "";
  display: block;
  left: 0;
  width: inherit;
  transition: background-color var(--trans-dur);
}
.pl__dot:before {
  animation-name: pushInOut1;
  background-color: var(--bg);
  border-radius: inherit;
  box-shadow: 0.05em 0 0.1em rgba(255, 255, 255, 0.2) inset;
  height: inherit;
  z-index: 1;
}
.pl__dot:after {
  animation-name: pushInOut2;
  background-color: var(--primary1);
  border-radius: 0.75em;
  box-shadow: 0.1em 0.3em 0.2em rgba(255, 255, 255, 0.4) inset, 0 -0.4em 0.2em #2e3138 inset, 0 -1em 0.25em rgba(0, 0, 0, 0.3) inset;
  bottom: 0;
  clip-path: polygon(0 75%, 100% 75%, 100% 100%, 0 100%);
  height: 3em;
  transform: rotate(-45deg);
  transform-origin: 50% 2.25em;
}

最後一步就是讓12個小圓圈按不同是時間間隔旋轉不同的角度,從而形成波浪式的起伏效果。

.pl__dot:nth-child(1) {
  transform: rotate(0deg) translateX(5em) rotate(0deg);
  z-index: 5;
}
.pl__dot:nth-child(1), .pl__dot:nth-child(1):before, .pl__dot:nth-child(1):after {
  animation-delay: 0s;
}
.pl__dot:nth-child(2) {
  transform: rotate(-30deg) translateX(5em) rotate(30deg);
  z-index: 4;
}
.pl__dot:nth-child(2), .pl__dot:nth-child(2):before, .pl__dot:nth-child(2):after {
  animation-delay: -0.1666666667s;
}
.pl__dot:nth-child(3) {
  transform: rotate(-60deg) translateX(5em) rotate(60deg);
  z-index: 3;
}
.pl__dot:nth-child(3), .pl__dot:nth-child(3):before, .pl__dot:nth-child(3):after {
  animation-delay: -0.3333333333s;
}
.pl__dot:nth-child(4) {
  transform: rotate(-90deg) translateX(5em) rotate(90deg);
  z-index: 2;
}
.pl__dot:nth-child(4), .pl__dot:nth-child(4):before, .pl__dot:nth-child(4):after {
  animation-delay: -0.5s;
}
.pl__dot:nth-child(5) {
  transform: rotate(-120deg) translateX(5em) rotate(120deg);
  z-index: 1;
}
.pl__dot:nth-child(5), .pl__dot:nth-child(5):before, .pl__dot:nth-child(5):after {
  animation-delay: -0.6666666667s;
}
.pl__dot:nth-child(6) {
  transform: rotate(-150deg) translateX(5em) rotate(150deg);
  z-index: 1;
}
.pl__dot:nth-child(6), .pl__dot:nth-child(6):before, .pl__dot:nth-child(6):after {
  animation-delay: -0.8333333333s;
}
.pl__dot:nth-child(7) {
  transform: rotate(-180deg) translateX(5em) rotate(180deg);
  z-index: 2;
}
.pl__dot:nth-child(7), .pl__dot:nth-child(7):before, .pl__dot:nth-child(7):after {
  animation-delay: -1s;
}
.pl__dot:nth-child(8) {
  transform: rotate(-210deg) translateX(5em) rotate(210deg);
  z-index: 3;
}
.pl__dot:nth-child(8), .pl__dot:nth-child(8):before, .pl__dot:nth-child(8):after {
  animation-delay: -1.1666666667s;
}
.pl__dot:nth-child(9) {
  transform: rotate(-240deg) translateX(5em) rotate(240deg);
  z-index: 4;
}
.pl__dot:nth-child(9), .pl__dot:nth-child(9):before, .pl__dot:nth-child(9):after {
  animation-delay: -1.3333333333s;
}
.pl__dot:nth-child(10) {
  transform: rotate(-270deg) translateX(5em) rotate(270deg);
  z-index: 5;
}
.pl__dot:nth-child(10), .pl__dot:nth-child(10):before, .pl__dot:nth-child(10):after {
  animation-delay: -1.5s;
}
.pl__dot:nth-child(11) {
  transform: rotate(-300deg) translateX(5em) rotate(300deg);
  z-index: 6;
}
.pl__dot:nth-child(11), .pl__dot:nth-child(11):before, .pl__dot:nth-child(11):after {
  animation-delay: -1.6666666667s;
}
.pl__dot:nth-child(12) {
  transform: rotate(-330deg) translateX(5em) rotate(330deg);
  z-index: 6;
}
.pl__dot:nth-child(12), .pl__dot:nth-child(12):before, .pl__dot:nth-child(12):after {
  animation-delay: -1.8333333333s;
}

另外,在12個小圓圈漸進式地變成小圓柱體的時候,圓柱體的陰影和高度變化運用到了下麵的CSS3動畫:

@keyframes shadow {
  from {
    animation-timing-function: ease-in;
    box-shadow: 0.1em 0.1em 0 0.1em black, 0.3em 0 0.3em rgba(0, 0, 0, 0.3);
  }
  25% {
    animation-timing-function: ease-out;
    box-shadow: 0.1em 0.1em 0 0.1em black, 0.8em 0 0.8em rgba(0, 0, 0, 0.5);
  }
  50%, to {
    box-shadow: 0.1em 0.1em 0 0.1em black, 0.3em 0 0.3em rgba(0, 0, 0, 0.3);
  }
}
@keyframes pushInOut1 {
  from {
    animation-timing-function: ease-in;
    background-color: var(--bg);
    transform: translate(0, 0);
  }
  25% {
    animation-timing-function: ease-out;
    background-color: var(--primary2);
    transform: translate(-71%, -71%);
  }
  50%, to {
    background-color: var(--bg);
    transform: translate(0, 0);
  }
}
@keyframes pushInOut2 {
  from {
    animation-timing-function: ease-in;
    background-color: var(--bg);
    clip-path: polygon(0 75%, 100% 75%, 100% 100%, 0 100%);
  }
  25% {
    animation-timing-function: ease-out;
    background-color: var(--primary1);
    clip-path: polygon(0 25%, 100% 25%, 100% 100%, 0 100%);
  }
  50%, to {
    background-color: var(--bg);
    clip-path: polygon(0 75%, 100% 75%, 100% 100%, 0 100%);
  }
}

到這裡,我們就完整地實現了這個純CSS3 3D載入動畫,文章最後也將源碼獻給大家。

源碼下載

完整的代碼我已經整理出了一個源碼包,供大家下載學習。

關註我的公眾號“前端技術官”,回覆關鍵字:3006,即可獲取源碼下載鏈接。

代碼僅供參考和學習,請不要用於商業用途。

最後總結

這個CSS3 3D Loading載入動畫主要運用了CSS3的動畫幀屬性、box-shadow屬性以及animation-delay屬性,希望本文對你的開發有所幫助。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 我們設計了一款分散式菜單應用,不需要個人去關註公眾號或下載小程式,服務員會提供幾個點單的平板,連接店鋪網路,區域網內通信,這樣大家點單、查看訂單詳情等都不受網路限制。 ...
  • 核酸檢測結果包含個人隱私數據且數據量較大,如何在雲端讓核酸檢測人員實現海量數據的安全存儲/查詢成為了核酸檢測數據存儲的首要難題。華為AppGallery Connect提供了認證服務和雲資料庫服務兩大Serverless服務,可分別實現用戶認證登錄、數據寫入/查詢等基本端雲協同功能,可完美解決核酸檢 ...
  • 近日,華為HMS Core手語服務攜手吉林大學、長春大學特教學院聯合打造暖心課堂,在直播網課中加入AI手語翻譯,於人文中融入科技,知識中融入溫暖。 手語翻譯:同學們大家好 HMS Core手語服務通過AI賦能教育,儘可能地為聽障人群提供更多優質的教學資源,讓他們能夠享受到更加公平、更高質量的公共教育 ...
  • 近期在做 epub.js 引擎解析電子書小項目,在閱讀界面通過電子書 rendition 對象的 touch 事件進行手勢翻頁功能時(圖1), 圖1 滑動頁面出現上下和左右方向上的空白部分以及會有回彈的效果(圖2), 圖2 剛開始感覺還挺好看的但後面越感覺越不對,這樣的用戶體驗個人感覺還不如固定視窗 ...
  • 作者:安小軒 原文鏈接:https://juejin.cn/post/7086272341994536974 實現一個旋轉的立方體,只需要用css的基本屬性就可以實現。我們一起看看吧~ 一:transform 基本屬性 transform可以實現元素的2D或3D轉換,可以對元素進行旋轉,縮放,移動, ...
  • 一、起因 最近在使用Umi進行React的前端開發,有一個數據表格分頁的功能需求,由於後端還沒完成所以考慮前端先使用Mock先來進行模擬數據測試。 Mock的介紹這裡就不做贅述,大家感興趣的可自行前往官網學習--Mock.js。 由於是分頁功能,必然少不了當前頁碼、分頁大小、過濾條件等請求參數,但是 ...
  • 講基礎不容易,本文通過 7個demo、6張圖、1.6k文字串講作用域鏈、詞法作用域、閉包、閉包使用案例。 ...
  • 一、什麼是協商緩存 協商緩存是伺服器端的一種緩存策略,服務端提供一種記號,用來判斷客戶端資源和服務端是否一樣。 一致返回304,否則返回200和新資源。 二、如何實現 主要是通過在response header中攜帶相關標識 一種通過last-modified資源的最後修改時間 第一次請求時,伺服器 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...