您好,登錄后才能下訂單哦!
這篇文章主要介紹怎么用CSS和GSAP實現樹枝發芽的loader動畫,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
代碼解讀
定義dom,容器包含2個元素,branch代表枝,leaves代表葉,葉有6個子元素,代表6個葉片:
<figureclass="sapling">
<divclass="branch"></div>
<divclass="leaves">
<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;
}
設置葉片的尺寸和和背景顏色:
.leavesspan{
width:2.5em;
height:2.5em;
background-color:limegreen;
}
設置左右葉片的各自樣式:
.leavesspan:nth-child(odd){
border-bottom-left-radius:3em;
border-top-right-radius:3em;
transform-origin:rightbottom;
align-self:flex-start;
}
.leavesspan:nth-child(even){
border-bottom-right-radius:3em;
border-top-left-radius:3em;
transform-origin:leftbottom;
align-self:flex-end;
}
至此,靜態效果繪制完成,接下來開始寫動畫腳本。
引入GSAP庫:
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.2/TweenMax.min.js"></script>
聲明一個時間線對象:
letanimation=newTimelineMax();
增加樹枝的入場動畫效果,并為這個動畫設置一個標簽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('.leavesspan',0.5,{scale:0,ease:Power1.easeOut},0.5,0.5,'branch');
增加葉片變黃的動畫效果:
animation.from('.branch',4,{scaleY:0,ease:Power1.easeOut},'branch')
.staggerFrom('.leavesspan',0.5,{scale:0,ease:Power1.easeOut},0.5,0.5,'branch')
.to(['.branch','.leavesspan'],3,{backgroundColor:'yellow'});
增加淡出效果:
animation.from('.branch',4,{scaleY:0,ease:Power1.easeOut},'branch')
.staggerFrom('.leavesspan',0.5,{scale:0,ease:Power1.easeOut},0.5,0.5,'branch')
.to(['.branch','.leavesspan'],3,{backgroundColor:'yellow'})
.to(['.branch','.leavesspan'],1,{autoAlpha:0});
修改聲明時間線的代碼,使動畫重復播放:
letanimation=newTimelineMax({repeat:-1,repeatDelay:0.5});
大功告成!
以上是“怎么用CSS和GSAP實現樹枝發芽的loader動畫”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。