使用純CSS實現一個圓環旋轉錯覺的動畫效果?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!
https://github.com/comehope/front-end-daily-challenges
代碼解讀定義 dom,容器中包含 10 個<div>
子元素,每個<div>
子元素內還有一個<span>
子元素:
<figure class="container"> <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實現一個圓環旋轉錯覺的動畫效果大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注創新互聯網站制作公司行業資訊頻道。
本文名稱:使用純CSS實現一個圓環旋轉錯覺的動畫效果-創新互聯
文章位置:http://m.2m8n56k.cn/article46/dccpeg.html
成都網站建設公司_創新互聯,為您提供面包屑導航、全網營銷推廣、動態網站、Google、ChatGPT、網站收錄
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯