本篇文章帶給大家的內容是關於如何使用CSS和GSAP實現樹枝發芽的loader動畫(附源碼) ,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
https://github.com/comehope/front- end-daily-challenges
定義dom,容器包含2 個元素,branch
代表枝,leaves
代表葉,葉有6 個子元素,代表6 個葉片:
<figure> <div></div> <div> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> </div> </figure>
居中顯示:
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: black; }
定義容器尺寸,並設定子元素水平居中:
.sapling { position: relative; width: 5em; height: 17.5em; font-size: 10px; display: flex; justify-content: center; }
畫出樹枝:
.branch { position: absolute; width: 0.2em; height: inherit; border-radius: 25%; background: burlywood; }
定義樹葉容器,設定為葉片在垂直方向均勻分佈,並且從下到上排列:
.leaves { position: absolute; width: inherit; height: 15em; top: 1em; display: flex; flex-direction: column-reverse; }
設定葉片的尺寸和背景顏色:
.leaves span { width: 2.5em; height: 2.5em; background-color: limegreen; }
設定左右葉片的各自樣式:
.leaves span:nth-child(odd) { border-bottom-left-radius: 3em; border-top-right-radius: 3em; transform-origin: right bottom; align-self: flex-start; } .leaves span:nth-child(even) { border-bottom-right-radius: 3em; border-top-left-radius: 3em; transform-origin: left bottom; align-self: flex-end; }
至此,靜態效果繪製完成,接下來開始寫動畫腳本。
引入GSAP 庫:
<script></script>
宣告一個時間軸物件:
let animation = new TimelineMax();
增加樹枝的入場動畫效果,並為這個動畫設定一個標籤branch
:
animation.from('.branch', 4, {scaleY: 0, ease: Power1.easeOut}, 'branch');
增加樹葉的入場動畫效果,它的參數中有3 個0.5
,從左到右的含義分別是動畫時長、多個葉片動畫的間隔時長、相對branch
標籤動畫的延遲時間:
animation.from('.branch', 4, {scaleY: 0, ease: Power1.easeOut}, 'branch') .staggerFrom('.leaves span', 0.5, {scale: 0, ease: Power1.easeOut}, 0.5, 0.5, 'branch');
增加葉片變黃的動畫效果:
animation.from('.branch', 4, {scaleY: 0, ease: Power1.easeOut}, 'branch') .staggerFrom('.leaves span', 0.5, {scale: 0, ease: Power1.easeOut}, 0.5, 0.5, 'branch') .to(['.branch', '.leaves span'], 3, {backgroundColor: 'yellow'});
增加淡出效果:
animation.from('.branch', 4, {scaleY: 0, ease: Power1.easeOut}, 'branch') .staggerFrom('.leaves span', 0.5, {scale: 0, ease: Power1.easeOut}, 0.5, 0.5, 'branch') .to(['.branch', '.leaves span'], 3, {backgroundColor: 'yellow'}) .to(['.branch', '.leaves span'], 1, {autoAlpha: 0});
修改聲明時間線的程式碼,使動畫重複播放:
let animation = new TimelineMax({repeat: -1, repeatDelay: 0.5});
大功告成!
相關推薦:
如何使用CSS和GSAP實作有多個關鍵影格的連續動畫(附原始碼)
如何用CSS純程式碼畫一個旋轉的太極圖(附程式碼)#以上是如何使用CSS和GSAP實作樹枝發芽的loader動畫(附原始碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!