這篇文章帶給大家的內容是關於如何使用純CSS實現一個圓環旋轉錯覺的動畫效果,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
效果預覽
#原始碼下載
#https://github.com/comehope/front-end-daily-challenges
程式碼解讀
定義dom,容器包含10 個<div> 子元素,每個<code><div> 子元素內還有一個<code><span></span>
子元素:
<figure> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> <div><span></span></div> </figure>
定義容器尺寸:
.container { width: 17em; height: 17em; font-size: 16px; }
定義子元素的尺寸,和容器相同:
.container { position: relative; } .container div { position: absolute; width: inherit; height: inherit; }
在子元素的正中畫一個黃色的小方塊:
.container div { display: flex; align-items: center; justify-content: center; } .container span { position: absolute; width: 1em; height: 1em; background-color: yellow; }
增加讓小方塊左右移動的動畫效果,動畫時間還會在後面用到,所以定義成變數:
.container span { --duration: 2s; animation: move var(--duration) infinite; } @keyframes move { 0%, 100% { left: calc(10% - 0.5em); } 50% { left: calc(90% - 0.5em); } }
用貝賽爾曲線調整動畫的時間函數,使小方塊看起來就像在左右兩側跳來跳去:
.container span { animation: move var(--duration) cubic-bezier(0.6, -0.3, 0.7, 0) infinite; }
增加小方塊變形的動畫,使它看起來有下蹲起跳的擬人效果:
.container span { animation: move var(--duration) cubic-bezier(0.6, -0.3, 0.7, 0) infinite, morph var(--duration) ease-in-out infinite; } @keyframes morph { 0%, 50%, 100% { transform: scale(0.75, 1); } 25%, 75% { transform: scale(1.5, 0.5); } }
至此,完成了1 個方塊的動畫。接下來設定多個方塊的動畫效果。
為子元素定義CSS 下標變數:
.container div:nth-child(1) { --n: 1; } .container div:nth-child(2) { --n: 2; } .container div:nth-child(3) { --n: 3; } .container div:nth-child(4) { --n: 4; } .container div:nth-child(5) { --n: 5; } .container div:nth-child(6) { --n: 6; } .container div:nth-child(7) { --n: 7; } .container div:nth-child(8) { --n: 8; } .container div:nth-child(9) { --n: 9; }
旋轉子元素,使小方塊分佈均勻地在容器的四周,圍合成一個圓形:
.container p { transform: rotate(calc(var(--n) * 40deg)); }
當設定動畫延時,現在看起來就像是一群小方塊貼著一個圓的內邊線在旋轉了(但實際上沒有任何元素在做旋轉運動,大腦感覺到的旋轉是一種錯覺):
.container span { animation-delay: calc(var(--duration) / 9 * var(--n) * -1); }
最後,為小方塊上色:
.container span { background-color: hsl(calc(var(--n) * 80deg), 100%, 70%); }
大功告成!想了解更多css知識,可以去php中文網css教學欄位去學習一下。
相關推薦:
以上是如何使用純CSS實現一個圓環旋轉錯覺的動畫效果(附源碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

我知道,我知道:有大量的內容管理系統選項可用,而我進行了幾個測試,但實際上沒有一個是一個,y&#039;知道嗎?怪異的定價模型,艱難的自定義,有些甚至最終成為整個&

鏈接CSS文件到HTML可以通過在HTML的部分使用元素實現。 1)使用標籤鏈接本地CSS文件。 2)多個CSS文件可通過添加多個標籤實現。 3)外部CSS文件使用絕對URL鏈接,如。 4)確保正確使用文件路徑和CSS文件加載順序,優化性能可使用CSS預處理器合併文件。

選擇Flexbox還是Grid取決於佈局需求:1)Flexbox適用於一維佈局,如導航欄;2)Grid適合二維佈局,如雜誌式佈局。兩者在項目中可結合使用,提升佈局效果。

包含CSS文件的最佳方法是使用標籤在HTML的部分引入外部CSS文件。 1.使用標籤引入外部CSS文件,如。 2.對於小型調整,可以使用內聯CSS,但應謹慎使用。 3.大型項目可使用CSS預處理器如Sass或Less,通過@import導入其他CSS文件。 4.為了性能,應合併CSS文件並使用CDN,同時使用工具如CSSNano進行壓縮。

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction

重構自己的代碼看起來是什麼樣的?約翰·瑞亞(John Rhea)挑選了他寫的一個舊的CSS動畫,並介紹了優化它的思維過程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!